Jump to content

Selector to omit templates that don't have a template file?


nghi
 Share

Recommended Posts

It doesn't. Whether a template file matching the name of the template (or the alternative name in the template config) is in fact present in the file system isn't stored in the database.

You could assemble a list of all templates without file and put that into a selector though:

$missing = array();
foreach($templates->find("flags!={template::flagSystem}") as $tpl) {
    if(! $tpl->filenameExists()) $missing[] = $tpl->name;
}
// Now you can get all pages with missing template file:
$pages->find("template=" . implode("|", $missing));
// Or just children
$page->children("template=" . implode("|", $missing));
  • Like 7
Link to comment
Share on other sites

  • 3 years later...
On 2/2/2016 at 6:09 PM, BitPoet said:

It doesn't. Whether a template file matching the name of the template (or the alternative name in the template config) is in fact present in the file system isn't stored in the database.

You could assemble a list of all templates without file and put that into a selector though:


$missing = array();
foreach($templates->find("flags!={template::flagSystem}") as $tpl) {
    if(! $tpl->filenameExists()) $missing[] = $tpl->name;
}
// Now you can get all pages with missing template file:
$pages->find("template=" . implode("|", $missing));
// Or just children
$page->children("template=" . implode("|", $missing));

Sorry, just stumbled across that older post searching for an answer regarding templates selectors...

I'm looking for a method to select all templates except system templates.

This part from your code above doesn't work here:

$templates->find("flags!={template::flagSystem}")

it could only get it to work like this:

$templates->find('flags!=' . Template::flagSystem)

But what's more important is: "flags" is a bitmask value, is it OK to simply compare it in a selector?

Link to comment
Share on other sites

Flags are only for internal use. You most likely cannot use them in selectors. Also you don't need to try to be efficient when selecting templates, as all templates are loaded as part of the processwire bootstrapping process, so you can simply iterate $templates and select only non system templates without using a selector. Everything's in memory anyways. 

  • Like 5
Link to comment
Share on other sites

3 hours ago, Gadgetto said:

But what's more important is: "flags" is a bitmask value, is it OK to simply compare it in a selector?

In Template context, is (currently) is, since the only flag currently defined for templates is the system flag. But @LostKobrakai is correct that iterating $templates isn't really a hardship (and likely faster anyway since there are no selectors parse).

  • Like 1
Link to comment
Share on other sites

4 hours ago, LostKobrakai said:

Flags are only for internal use. You most likely cannot use them in selectors. Also you don't need to try to be efficient when selecting templates, as all templates are loaded as part of the processwire bootstrapping process, so you can simply iterate $templates and select only non system templates without using a selector. Everything's in memory anyways. 

Ok, thanks! Solved it like this:

/**
 * Get all templates except system templates (name => label)
 * 
 * @return WireArray $templates
 * 
 */
public function getTemplates() {
    $templates = new WireArray();
    foreach ($this->wire('templates') as $t) {
        if (!($t->flags & Template::flagSystem)) {
            $templates->add($t);
        }
    }

    return $templates;
}

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...