You can also take advantage of the built-in Service Parameters system, which lets you isolate or reuse the value:
# app/config/parameters.yml parameters: ga_tracking: UA-xxxxx-x # app/config/config.yml twig: globals: ga_tracking: "%ga_tracking%" Now, the variable ga_tracking is available in all Twig templates:
<p>The google tracking code is: {{ ga_tracking }}</p> The parameter is also available inside the controllers:
$this->container->getParameter('ga_tracking'); You can also define a service as a global Twig variable (Symfony2.2+):
# app/config/config.yml twig: # ... globals: user_management: "@acme_user.user_management" http://symfony.com/doc/current/cookbook/templating/global_variables.htmlhttp://symfony.com/doc/current/templating/global_variables.html
If the global variable you want to set is more complicated - say an object - then you won't be able to use the above method. Instead, you'll need to create a Twig Extensioncreate a Twig Extension and return the global variable as one of the entries in the getGlobals method.