doolak Posted January 15, 2013 Posted January 15, 2013 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...
Pete Posted January 15, 2013 Posted January 15, 2013 There's nothing wrong with your final option, except my personal preference is && instead of "and" or "AND".
doolak Posted January 15, 2013 Author Posted January 15, 2013 I just thought that there is a PW-specific solution for this which may be suitable e.g. if you have a lot of selectors which you want to exclude...
Pete Posted January 15, 2013 Posted January 15, 2013 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 }
doolak Posted January 15, 2013 Author Posted January 15, 2013 Yes, that does not work - same as my attemps above. But i guess there is just a little mistake in thinking somehow...
Pete Posted January 15, 2013 Posted January 15, 2013 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
doolak Posted January 15, 2013 Author Posted January 15, 2013 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... ;-)
doolak Posted January 15, 2013 Author Posted January 15, 2013 Uhh, i tried $page->matches, but not as you described it with !$page - that looks good, i will have a try!
Pete Posted January 15, 2013 Posted January 15, 2013 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
doolak Posted January 15, 2013 Author Posted January 15, 2013 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)) && ...
Pete Posted January 15, 2013 Posted January 15, 2013 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!
doolak Posted January 15, 2013 Author Posted January 15, 2013 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 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now