Caching, Changes to Render + More Caching
Huge improvements coming in this release of Timber. Here's the big stuff:
Caching:
@mgmartel did some great work on caching. Now when go to render, you can set an expires time (in seconds):
Timber::render('single.twig', $data, 1200)
There are several different caching methods you can send:
Timber::render('single.twig', $data, 1200, TimberLoader::CACHE_USE_DEFAULT)
Timber::render('single.twig', $data, 1200, TimberLoader::CACHE_SITE_TRANSIENT)
Timber::render('single.twig', $data, 1200, TimberLoader::CACHE_TRANSIENT)
Timber::render('single.twig', $data, 1200, TimberLoader::CACHE_OBJECT)
... if you send nothing (as the first example) it will default to CACHE_USE_DEFAULT
which is actually transient. WordPress transients first use a persistent object cache (if you have it enabled) or fallback to using the database cache. I've noticed great performance boosts from this, giving me 50% - 65% decrease in rendering time. There are slight benefits to object cache (speed-wise), but the gains are very similar for DB cache
In-Template Caching
You can also use the {% cache %}
tag inside of a .twig
file to cache parts of the template. @mgmartel explains the details
Changes to Timber::render
Warning: Breaking Change That said, this is an edge-case. It used to be that the third argument for Timber::render
was $echo (which defaulted to true). In some circumstances you might have had it just return the rendered template w/o echo'ing so that you could store it separately. Now, you can do this with Timber::compile
:
$megamenu = get_my_mega_menu();
$data['megamenu_rendered'] = Timber::compile('megamenu.twig', $megamenu);
Timber::render('page.twig', $data, 3600);
Post next/prev
@thisislawatts contributed next/prev post methods. Inside of a post object you can do:
{{post.get_next}}
... to return the next post (and also do {{post.get_next.title}}
, etc.)
Misc
- @daslicht made
post.get_preview
a little simpler for when you want to omit the "read more" - Misc cleanup to keep making things more consistent from object-to-object.
- Some small internal caching things to prevent extra queries