Jump to content

Check if ($page->name - - - - ? ) is within a range of page names


pwired
 Share

Recommended Posts

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

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

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.

  • Like 3
Link to comment
Share on other sites

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
}
  • Like 2
Link to comment
Share on other sites

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

  • 4 weeks later...

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

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