Symfony News

New in Symfony 2.7: Twig Profiler

Fabien Potencier

Contributed by
Fabien Potencier
in #13428.

In Symfony project we believe that all the information relevant to developers should be one click away. That's why in Symfony 2.7 we've added a new panel with Twig information in the web debug toolbar.

Currently, the information about the resources used to render each template is displayed in the Timeline panel of the Symfony profiler:

Twig Basic Profiler

Sadly, this panel doesn't provide much detail for templates. Moreover, some templates aren't registered, so their information is missing in that panel.

Symfony 2.7 solves these problems thanks to the new profiler introduced by Twig in its 1.18.0 version. First, you'll see a new Twig debug panel in the web debug toolbar that gives you a quick overview of the main Twig stats:

Twig Debug Panel

If you click on that panel, you'll see the full information collected by the Twig profiler. First, you'll get the main Twig stats for the page, which is the first thing to check when things go wrong:

Twig Profiler Stats

Second, you'll see the full list of templates used to render the page, including the templates of third-party bundles and the template fragments:

Twig Profiler Templates

Lastly, you'll get the full call graph executed by Twig to render the page. This information is mostly useful when things go wrong or your templates render slowly. Thanks to the detailed information, you'll discover the performance bottlenecks instantly:

Twig Profiler Call Graph

Symfony 2.7 enables the Twig profiler automatically when the application uses the debug mode. If you are using Twig outside of Symfony, you must register the new profiler extension before rendering the templates:

1
2
3
4
5
6
7
$profile = new Twig_Profiler_Profile();
$twig->addExtension(new Twig_Extension_Profiler($profile));

// render templates ...

$dumper = new Twig_Profiler_Dumper_Text();
echo $dumper->dump($profile);

In addition to the basic text dumper (which is the one used by Symfony), Twig also includes an advanced dumper to output the call graph information in a format compatible with the Blackfire Profiler.


Be trained by Symfony experts - 2015-04-14 London - 2015-04-14 London - 2015-04-16 London


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook