Jump to content

using $page->render($options) and template cache


Soma
 Share

Recommended Posts

Template Cache and $page->render($options)

If you ever use the $page->render() to render out partials (of another page using its template file) and use template cache for the pages you're going to render and the page where you render it, it will create a cachefile. So if you go to that previously rendered and cached page, it will render that partial. If the page is accessed before a cache is created, it will cache this one and render that in as the partial, so kinda turned around. :) Funny effect. And many mmms and oaaahhhs :)

To get a better understanding what's happening read on.

Simple example code from a list page to render partials of articles (likely)

// from the list pages template

$markup = '';
foreach($products as $key => $child) {
    $markup .= "<dl>";
    $markup .= $child->render(array('isOverview' => true, 'class' => $class));
    $markup .= "</dl>";
}

echo $markup;

And in the template of the article

// in article template file

if(isset($options['isOverview']) && $options['isOverview'] == true) {
    
    // render small partial 
    
    $class = $options['class'];

    $markup = "<dd class='$class'>
        <h4>$page->title</h4>
        <p>$page->summary</p>
        <a href='$page->url'>details</a>
        </dd>";

} else {
    // render complete article
    $markup = "<div class='product-details'>
        <h1>$page->title</h1>
        $page->body
        </div>";
}

// output
echo $markup;

So now the render call 

$markup .= $child->render( array('isOverview' => true, 'class' => $class) );

in the list template will cache the page it renders (the small view of it). Thus if you access the page directly it will serve the cached small view of it. Ups.

Solutions

This is without specifying a different template file in the first argument in the render(). The effect doesn't happen when you, let's say create a new template file (ie article-small.php) and use that to render the page. Since this new template file is not connected to the template in PW it also has no cache for that render.

To show what I mean is the following with the first argument the file you want the view to render.

$markup .= $child->render("product-small.php", array("isOverview" => true, "class" => $class));

Still following me? Ok there's also another method to not allow a cache file to be created. There's a default options set in the render() in PW. Bingo! allowCache is what we can also use.

$markup .= $child->render("product-small.php", array(
     "allowCache" => false,
     "isOverview" => true,
     "class" => $class
    ));
 
And everything's back to normal.

Just wanted to write down a little thing, as I stumbled over this recently, to scatter some keywords about this here ;) . I think this isn't really documented somewhere but I thought it was maybe mentioned by Ryan in a thread about when he added this feature: http://processwire.com/talk/topic/3145-multiple-views-for-templates/page-2?hl=%2Brender+%2Bcaller#entry32876.

Edit:

Zaaakkkk and it's in Google

9 minutes ! :)

  • Like 18
Link to comment
Share on other sites

  • 2 months later...
  • 6 months later...

Is this still working with PW 2.4?

I am creating a new template – as email body – within a for loop. But it looks like all email bodys/messages are the same and I think it is because of the cached file.

foreach($subscribers as $subscriber) {

$filename = dirname(__FILE__) . "/mail-template.php";
$t = new TemplateFile($filename);
$t->set('articles', $articles);
$body = $t->render('', array("allowCache" => false));

// Send email …
}
Link to comment
Share on other sites

  • 5 months later...
  • 3 years later...

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