[Symfony 2][Twig] – Enabling (native) Twig Extensions

Twig Extensions is a tiny official repository for extensions to the Twig templating markup language, the default templating engine in each Symfony 2 (Standard Ed.) project. This short article shows how to purposeful enable them per-environment for your projects.

The twig-extension repository is usually fetched by calling

$ bin/vendor install

into your project´s ./vendor directory (because it is part of the ./deps file of each symfony 2 standard edition).

After installing the required vendor files, register the namespace fallback in your autoload.php (if you use the one delivered with Symfony 2 Standard, this should already have been done for you):

use Symfony\Component\ClassLoader\UniversalClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
// ...
));
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
'Twig_'            => __DIR__.'/../vendor/twig/lib',
));

Remember that twig extensions are always registered as tagged services, but it is not always desirable to register a twig extension like e.g. the Debug-Extension as a regular service globally. We could do so by adding the service definition to the services.xml-file in one of our bundles. But due to the special purpose of the debug extension, we will choose a different, more “project-wide-configuration”-way.

Luckily, Symfony provides means to dynamically load services “on demand” and depending on the current environment.

Add the following lines to your app/config/config_dev.yml:

services:
debug.twig.extension:
class: Twig_Extensions_Extension_Debug
tags: [{ name: 'twig.extension' }]

The “services”-configuration option may be utilized to register any class as a DIC managed service, for each project and independent from your bundle configuration (But remember that “regular” service definition configuration should usually happen in your bundle´s configuration files, otherwise you will not able to distribute your bundles because of missing or broken dependencies).

For twig extensions that should only be loaded for special purposes (like our DEBUG-function which is only needed in development environments), config_dev.yml is the perfect place to register them.

Note the “tags”-configuration which marks the service as a twig extension. Now, let´s create a demo twig-template and try the {% debug %} function:

{% extends "NerdpressDemoBundle::layout.html.twig" %}
{% debug %}

That will do. Open the page in your favorite webbrower (after defining a route, a controller and other stuff i won´t mention here) and admire the debug output. Note that the output will be much more nicer if you have had installed a native php debug extension like e.g. XDebug.

Leave a Reply

Your email address will not be published. Required fields are marked *