Jump to content

Use templateFile class with cache


pwFoo
 Share

Recommended Posts

Hi,

I use TemplateFile class to render custom files like _layout.tpl (base html layout file), but output I think output isn't cached. Because there is no template added to PW I can't configure template cache I think.

How to activate template cache inside of php code temporarily or persistent (for example with a hidden / system template created)? 
Should be done with code (module install or inside a php file).

Link to comment
Share on other sites

Template cache is bound to the template of the current page. When rendering a page, regardless of how many files you render or include, only the markup that is output to the browser will be cached into a file (can be found in /site/assets/cache/Page/<id>), so that the render routine can be skipped if request can be responded with the cached output.

If you want to cache arbitrary bits of markup you can use WireCache class with $cache API variable, or use MarkupCache module.

$layout = $cache->getFor($page, 'layout');
if (!$layout) {
    $layout = $files->render('layout.tpl', [
        'title' => $page->title,
        'content' => $page->body
    ]);
    $cache->saveFor($page, 'layout', $layout, WireCache::expireDaily);
}

One thing to note is that template cache, by default, is disabled when you're logged in.

https://processwire.com/api/ref/wire-cache/

https://processwire.com/talk/topic/7-new-markupcache-module/

 

Edited by abdus
better wording
  • Thanks 1
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

×
×
  • Create New...