Jump to content

Using selectors instead of AND / OR ?


doolak
 Share

Recommended Posts

Well, i guess i just have to make a break and catch some sleep...

I want to include a file just if the current page does not use specific templates, but i cannot get it to work with PW selectors somehow in the way like this:

$select = $pages->find("template=news|news_article");
    if ($page->id != $select) {
        include ("./recent_news.inc");
    }

or maybe:


if ($page->template != news|news_article) {
include ("./recent_news.inc");
}

I know i am running somehow in the wrong direction, so for now i am using this:

if (($page->template!=news) and ($page->template !=news_article)) {
include ("./recent_news.inc");
}

... which works, but it does not make me happy, because i know there has to be a different approach.

Would be so great if you please push me in the right direction...

Link to comment
Share on other sites

It does sound like you should be able to do this from reading the API pages on Selectors, but I can't get it to work and it certainly would be neater:

if ($page->find('template!=template1|template2') {
    // Do your thing
}
Link to comment
Share on other sites

I just tried this, which works for ONE template, but the docs suggest it should work with a selector:

if ($page->is('template!=template1')) {

Found that on this page: http://processwire.com/api/variables/page/

I need to read the full docs sometime - I've still not done that yet and there is a LOT in there :)

But still, that doesn't do what you're attempting!



In fact, according to that page, $page->find will search children, grandchildren etc but not the current page.



Here's the answer:

if (!$page->matches('template=template1|template2')) {

Try that one :)

Link to comment
Share on other sites

LOL - yes, and using template1|template2 works like OR - so that's why i tried my first described solution.

But it works fine as it is now - so no need to spend too much time with this - it's just as you said, PW is full of possibilities and it just does not go out of my mind because i know there is a very simple solution somewhere in the API... ;-)

Link to comment
Share on other sites

It seems to work for me, but only with the ! at the front, which actually makes more sense when you read it:

"If not page matches template template1 or template2"

makes more sense than

"If page matches template not template1 or template2"

The second one sounds like WillyC :)

Link to comment
Share on other sites

Perfect! Works like a charm.

And imagine the code without the API if you have a lot of exclusions like this:

if ($page->find('template!=template1|template2|template3|template4, name=Peter|Paul|Mary, age>21) {
    // Do your thing
}

It would be a quite long repetition of if (($page->template!=news) && ($page->template !=news_article)) && ...

Link to comment
Share on other sites

That's why I wanted to work this one out - I've got a lot of repetition in some templates on one site so this will streamline things nicely!

Exactly.

The second one sounds like WillyC :)

agree i.do

do.i thank y.ou so much

  • 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...