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:

  1. Thank you, you enlightened my day.

    ReplyDelete
  2. Thanks you helped me too!

    ReplyDelete
  3. FUCK YEAH AS WELL! Thanks!!!

    ReplyDelete
  4. 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 }}

    ReplyDelete
  5. Anonymous11:10 AM

    Thx a lot man :-)

    ReplyDelete
  6. Anonymous3:32 AM

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

    ReplyDelete
  7. Anonymous6:00 AM

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

    ReplyDelete
  8. khadija TW4:26 AM

    thanks you helped me too ^_^

    ReplyDelete
  9. Anonymous12:30 PM

    thank you very much!!!!

    ReplyDelete
  10. Anonymous3:45 AM

    works like a charm indeed!

    ReplyDelete
  11. Anonymous12:02 AM

    Great, + ;)

    ReplyDelete
  12. Anonymous5:40 AM

    thx!

    ReplyDelete