Jump to content

Creating a page sibling if statement


Jamie Gordon
 Share

Recommended Posts

Hi guys! I've only recently started using PW but I'm finding it a great tool in content managing my websites. Just a quick question. I have created a series of pages in categories. These sibling pages use different templates, is there a way to check if a sibling page uses a particular template and if so also check a textarea field for a particular piece of text so that I can display it? Any help would be greatly appreciated.

Jamie

Link to comment
Share on other sites

Hi Jamie, and welcome. Have you seen the cheatsheet yet: http://cheatsheet.processwire.com/ I am not sure if you are looking to check the template of a specific page, or find a sibling with a specific template. This should help:

if($page->template == 'template_name'){
As for checking is a textarea (or any other field contains a specific piece of text)
if (strpos($page->textareafieldname, 'specifictext') !== FALSE) echo 'Found';else echo "Not found";
Hope that helps.
  • Like 1
Link to comment
Share on other sites

Hi Jamie. Welcome to PW!

In a bit of a hurry but this page also has answers for you - http://processwire.com/api/selectors/. E.g.

title|body*=sushi tobiko

//would match only those pages that had the exact phrase "sushi tobiko" present in either their title or body fields

Edit: Adrian is fast! 

- As you read the docs (and this is mentioned there) $page always refers to the current page. $pages alwasy refer to all other pages

Link to comment
Share on other sites

But kongondo might have a better answer for finding text within a field. I really depends on how you'd be using it. Whether you want to find pages with that text (his example), or whether you want to do something different with the output in your template depending on whether that text exists or not when you are displaying a page (my example).

Hopefully one of us has hit the mark :)

Link to comment
Share on other sites

In PW 2.3 all jQuery traversing methods are available. So not yet in the cheatsheet is things like prevAll(selector) or nextAll(selector), nextUntil(selector) ...

For what you ask I think this is what you need:

$found = $page->siblings("template=mytemplate,body*=your text")->first();

if($found->id) {
    echo $found->body;
}

Nothing to explain really, except that sibling() is like find() and returns an PageArray of pages even if only one found. So to get the first from the array you add ->first. This will return on the one page and you can make sure it has one found with if($found->id){...

Edit: ok maybe another one on a sidenote, when working with $page->siblings(), it will also return all including the page you're calling this from. So you could add a selector "id!=$page->id" to exclude it.

  • Like 3
Link to comment
Share on other sites

Wow, I didn't expect this kind of reponse or support!

You guys are amazing! Soma your advice worked perfectly, but thank you all for trying to help. :)

Another quick question related to this, since I can search for a specific piece of text, would it be possible to search for one of the current pages field names?

For example -

$found = $page->siblings("template=course,hotels_served*=$page->my_name");

Obviously this piece of code doesn't work, but is that something similar?


 

Link to comment
Share on other sites

I'm not following. Do you want to search for a field name or search the contents of a field of the current page?  

$page->nameOfYourField

Gets you the current page's field with that name. E.g. $page->title give's your the current page's title field (assuming it has a field with the name "title"  ;)). So you can do echo $page->title; 
 
On the other hand $page->fields gives you an array of the fields attached to the current page (via its template). This is if you want the names of those fields. With arrays, you do a foreach if you want to echo them out, for instance.

Edit: Also useful if you can state what you want to accomplish. In many cases, there are easier ways of going about tasks in PW... O0

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

I was able to solve the problem last night.

Basically I wanted the current page to look through a sibling page with a specifc template and search for a field that matched the current page's name.

I was able to use this piece of code, for future reference :)

$found = $page->siblings("template=course,hotels_served%=$page->hotel_name");
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...