Jump to content

Page rendering


Joss
 Share

Recommended Posts

Sorry, very quick and silly question.

On a template, how do you call in a specific page and render it?

I know you can call it by id <?php echo $pages->get(1036)->render(); ?>

And I know you can get the children,

But can you call it just by name?

This is for the wiki.

EDIT: And another question, is there a way of finding the page ID other than editing it? (And getting the ID from the address bar)

Joss

Link to comment
Share on other sites

You can use the same selectors with get() than you'd use with find(). So for example getting a page called "footer" under page /blocks/ would be something like:

$pages->get("parent=/blocks/, name=footer")

When using get(), if the selector matches more than one page, only the first match will be returned.

  • Like 1
Link to comment
Share on other sites

Thanks Nik,

So does it require a relationship unless it is called by id?

The bit in the WIki I am working out is all about calling in pages (and rendering them) into another page.

http://wiki.processwire.com/index.php/Including_a_page_in_another_page (only part done)

So for the moment I am covering:

  1. Calling in a single page and rendering it directly in a template file.
  2. Calling in a single page via a "Page field" and rendering it
  3. Calling in a series of child pages and rendering them directly in a template file
  4. Calling in a series of pages via a pages field and rendering them.

I have 2 and 3 sorted, have not thought about 4 yet and am a little puzzled by 1.

I am being careful to explain in which circumstances if you move a page, you mess up your system, and when it is okay, for instance calling by ID.

  • Like 4
Link to comment
Share on other sites

Thanks Martijn

Okay, so there is a relationship there. If it is moved from /blocks/ it will break.

Which reinforces the point I am making that unless this is part of your template structure (for some reason) it is better to pull a page in via a Page Field so you are not messed up when it moves and you can change the source page easily.

Ta much!

(PS: Last time I met someone called Martijn, I wrote a whole pile of music for his fantastic animations for the Dutch government. Good money too!)

  • Like 1
Link to comment
Share on other sites

You can call only by the name, but there is the risk of matching more than one pages because you are allowed to give the same name to different pages if they're not under the same parent.

Link to comment
Share on other sites

Joss, there are many many ways to find Page / PageArrays.

$pagesAboutAntti = $pages->find("title*=Antti"); // It does "break" only if title is changed
$anttisChildren = $pages->get("name=antti-peisa")->children(); // It search for page which name is antti-peisa and returns all it's children
$personCalledAntti = $pages->get("template=person, name=antti"); // This one I use a lot - combined with family settings it is pretty much bulletproof

Btw: I myself almost never use the $somePage->render() - I have found very little use for it (for widgets/blocks maybe). So much more common case is to grab a page and output only what is needed, like this:

$latestProduct = $pages->get("template=product, sort=-created");
echo "<a href='$latestProduct->url'>$latestProduct->title $latestProduct->price €</a>":
Link to comment
Share on other sites

Diogo - That is a very good point! Thanks

Apeisa - Yes, I have been using for blocks in boostrap templates, but I agree, it is better to be more focused and pull specific information in other uses.

I suspect that the "Pages" section of the Wiki and any subsequent manual is liable to become the most complicated and thus, the hardest to understand.

It really needs to be broken down into specifics - use this for this situation - so that users can make a really good judgement on which way to go.

Perhaps it could do with a sort of Best Practices article that lists the methods without the fine details so you can browse through and say "that is the sort of thing I want to achieve" and follow the link through to the detail.

You guys up to helping on that?

Joss

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