Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache block with twig

I've switched from Phptal to Twig: very better experience. However, in phptal I did "tal:cache" to cache some blocks of code... with Twig, how can I accomplish that?

like image 370
Oscar Fanelli Avatar asked Sep 29 '12 20:09

Oscar Fanelli


2 Answers

You can cache blocks in twig with this extension:

https://github.com/asm89/twig-cache-extension

It allows you to cache blocks of a template based on TTL, a changing cache key, etc.

like image 147
asm89 Avatar answered Nov 13 '22 18:11

asm89


In twig there is no explicit caching of certain blocks.

But twig caches the intermediate code created from the templates, when not in debug mode.

See here:
http://symfony.com/doc/current/book/templating.html#twig-template-caching

This is in symfony context but i guess this would also work standalone.
You then can specify it with the following while instancing Twig_Environment:

'cache'       => 'cache/templates', // the place to cache to
'auto_reload' => true //reload template when changes are detected

See here:
http://twig.sensiolabs.org/doc/api.html#environment-options

like image 26
ivoba Avatar answered Nov 13 '22 16:11

ivoba