pwired Posted May 6, 2013 Share Posted May 6, 2013 Hi, I have succes with this code in a template file. It checks if the name of the current page = intro1 and if yes then do some stuff. if($page->name == "intro1") {echo "<div class='wrapper'>\n";echo "<div id='myGallery' class='spacegallery'>\n";foreach($page->images as $image) { echo "<img src='$image->url' alt='$image->description'>\n";}echo "</div>\n";echo "</div>\n";} My question is how can I check for a range of pages like: if $page is within that range, then do some stuff. I have 8 pages with following names: slideshow18 - slideshow28 - slideshow38 - - - - slideshow88. So I want to check with if($page->name == "slideshow18 or slideshow28 or slideshow38 - - - slideshow88") { do stuff The above code is of course wrong, I just wrote it with "or" between the names just to make my question more clear. I know this is easy for most of you guys here but I can't find it. Is it even possible to do it with $page->name or do I have to use $page->id with <= and >= Thanks. Link to comment Share on other sites More sharing options...
diogo Posted May 6, 2013 Share Posted May 6, 2013 There are some ways of doing this. I like this one: if ($pages->find("name=slideshow18|slideshow18|slideshow38|slideshow88")->has($page)) Link to comment Share on other sites More sharing options...
pwired Posted May 6, 2013 Author Share Posted May 6, 2013 Thanks Diogo, I am going to try that immediately. Link to comment Share on other sites More sharing options...
teppo Posted May 6, 2013 Share Posted May 6, 2013 You could also do this: if ($page->is("name=slideshow18|slideshow28|slideshow38") { I prefer this method, since it's slightly shorter, bit more readable and doesn't need to fetch any additional pages just for comparison. 3 Link to comment Share on other sites More sharing options...
diogo Posted May 6, 2013 Share Posted May 6, 2013 @teppo, better for sure. Forgot that one Link to comment Share on other sites More sharing options...
Soma Posted May 6, 2013 Share Posted May 6, 2013 Maybe simpler to do this: if($page->is("name=slideshow18|slideshow28|slideshow38|slideshow88")){ // page has one if the names } Or if it doesn't matter what number if($page->is("name^=slideshow")){ // page name starts with slideshow } 2 Link to comment Share on other sites More sharing options...
pwired Posted May 6, 2013 Author Share Posted May 6, 2013 Diogo - Teppo, both your solutions are working, thanks ! Soma wrote: Or if it doesn't matter what number if($page->is("name^=slideshow")){ // page name starts with slideshow Yes Soma, you're right it doesn't matter what number, as long as it is in the range of the given names Your solution is also working Thanks everybody. With your help I can now output a slideshow conditionally via a template file (controller ?) and don't have to use 8 static html files anymore. Link to comment Share on other sites More sharing options...
Pete Posted June 2, 2013 Share Posted June 2, 2013 Of course, you could just as easily have created a new template for slideshows called "slideshow" and in the page settings for each of those pages changed the template from "basic-page" to "slideshow" rather than doing conditional logic based on page names. Might be less confusing all round unless there was a specific reason you were doing it this way? More on templates here, specifically the third paragraph: http://processwire.com/api/templates/ Link to comment Share on other sites More sharing options...
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