Sunday, June 19, 2011

How to enable the twig truncate filter (Text extension) in Symfony2 beta5


Problem ?


In previous versions of Symfony2, to enable a extension you needed to add the Text and/or Debug extensions to an "extensions" array in your config.yml

//app/config/config.yml
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
extensions:
- twig.extension.debug
- twig.extension.text



This was because the extensions were added to the twig.xml file


//vendor/symfony/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
...
<service id="twig.extension.text" class="Twig_Extensions_Extension_Text" public="false">

<service id="twig.extension.debug" class="Twig_Extensions_Extension_Debug" public="false">
...




But, this is the way to do it now :

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


I spent some time trying to figure out how to do it, so I hope it helps someone.

13 comments:

Quentin said...

Thank you, you enlightened my day.

VincentG.Me said...

Thank you so much !

Web Hosting said...

Thanks you helped me too!

KaroDidi said...

FUCK YEAH AS WELL! Thanks!!!

Thomas IJ said...

Thats cool. I just wanted to know how you truncate content with html tags and prevent mayhem on the webpage.

{{ newsarticle.content | truncate(200) | raw }}

Anonymous said...

Thx a lot man :-)

Anonymous said...

Thank you, thank you, thank you! You saved my day.

Anonymous said...

Thanks a million, mate. You saved me a couple of hours

khadija TW said...

thanks you helped me too ^_^

Anonymous said...

thank you very much!!!!

Anonymous said...

works like a charm indeed!

Anonymous said...

Great, + ;)

Anonymous said...

thx!