How to get config parameters in Symfony2 Twig Templates

Timo Haberkern picture Timo Haberkern · Jul 22, 2011 · Viewed 160.2k times · Source

I have a Symfony2 Twig template. I want to output the value of a config parameter in this twig template (a version number). Therefore I defined the config parameter like this:

parameters:
    app.version: 0.1.0

I'm able to use this config parameter in Controllers but I have no clue how to get it in my Twig template.

Answer

Ryall picture Ryall · Dec 2, 2011

You can use parameter substitution in the twig globals section of the config:

Parameter config:

parameters:
    app.version: 0.1.0

Twig config:

twig:
    globals:
        version: '%app.version%'

Twig template:

{{ version }}

This method provides the benefit of allowing you to use the parameter in ContainerAware classes as well, using:

$container->getParameter('app.version');