How to speed up template rendering in Symfony2? How to speed up template rendering in Symfony2? symfony symfony

How to speed up template rendering in Symfony2?


When in production mode, Symfony2's Twig template rendering is actually done using cached templates, meaning that it is a PHP template; a Twig template compiled into pure PHP code. In dev mode, it of course doesn't cache the templates like that, but compiles it each time. Read more about it here, in the docs.

It will take PHP some time to compile a page with that much data, even when the template is just the normal PHP-HTML infusion code that tends to perform fastest. - You may want to look into normal HTTP caching, like Varnish, if this page is something that doesn't have to be rendered from the database each time.

As for the profiling. Something like XDebug should get you more detailed profiling info than what is built into Symfony2.


6K of iterations is a lot of work.

Idea #1:

Can you implement data pagination? That seems highly reasonable...

Idea #2 (thick client):

Implement some JavaScript templating mechanism. Your controller should then return JSON in order for JS to go though it and render it completely.

Even in this situation, relying of browser's speed to do 6K loops fast is lot to ask. You will need to implement pauses in between (like, after every 150th iteration) so the browser doesn't go into non-responsive mode...


It sounds like you are need to use PHP twig extension, which was made for your situation.

From docs:

And from now on, Twig will automatically compile your templates to take advantage of the C extension. Note that this extension does not replace the PHP code but only provides an optimized version of the Twig_Template::getAttribute() method.

I personally didn't get almost any visible impact, when I tried it a year ago for my project. Please share your experience, if you use it.