Cache block with twig
Asked Answered
B

2

5

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?

Blockhead answered 29/9, 2012 at 20:36 Comment(0)
D
6

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.

Drift answered 10/12, 2013 at 19:23 Comment(3)
is it compatible with ZF2 modules?Blockhead
As far as I know there is no module integrating it with ZF2, it's just a couple of services and an extension to add to twig though. :)Drift
@OscarFanelli go for it! Make a module for it, or patch the original repo itself if it's supposed to be also a moduleProtection
T
4

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

Telescope answered 3/10, 2012 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.