Symfony 2: Enable Twig Extensions

The other day I was dicking around with some code and I felt like I needed to truncate a string.

Fortunately I was working with Symfony, which uses Twig templates, which is definitely my preferred templating system in 2014.

But by default Symfony’s Twig extensions, which give you stuff like a truncate filter, is not enabled.

It’s easy to get turn it on though, just add this to your services.yml file.

// app/config/config.yml
services:
 twig.extension.text:
     class: Twig_Extensions_Extension_Text
     tags:
         - { name: twig.extension }

Once you have the Twig extensions loading, you can do stuff like this.

{{ user.middlename|truncate(1) }}
Screen shot 2015-11-22 at 8.53.10 PM
Share Button