Tag Archives: gettext

Twig-extensions in Symfony2

Twig comes with multiple filters that provide functions like round, slice, sort, etc. In case you need more filters Twig-extensions becomes handy because it implements all those filters that are not available by default:

  • Debug: Provides tags and filters to ease template debugging.
  • Text: wordwrap, truncate and nl2br for Strings.
  • i18n: Adds internationalization support via the gettext library.
  • Intl: localizeddate provides localization of DateTime objects.
  • Array: shuffle filter.

First you have to check if these Twig-extensions are already installed. Open composer.json and look for this:

    "require": {
        ...
        "twig/extensions": "1.0.*",
        ...
    }

If you don’t have that, add it and run in the terminal:

composer update

Then you have to enable this extensions in config.yml:

services:
    twig.extension.text:
        class: Twig_Extensions_Extension_Text
        tags:
           - { name: twig.extension }

From now on you can use these extensions as usual in Twig templates:

    {{ name|truncate(10) }}

Ref: https://github.com/fabpot/Twig-extensions