How to Inject Variables Automatically into all Templates

How to Inject Variables Automatically into all Templates

Twig allows to inject automatically one or more variables into all templates. These global variables are defined in the twig.globals option inside the main Twig configuration file:

  • YAML

    1. # config/packages/twig.yaml
    2. twig:
    3. # ...
    4. globals:
    5. ga_tracking: 'UA-xxxxx-x'
  • XML

    1. <!-- config/packages/twig.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:twig="http://symfony.com/schema/dic/twig"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/twig
    9. https://symfony.com/schema/dic/twig/twig-1.0.xsd">
    10. <twig:config>
    11. <!-- ... -->
    12. <twig:global key="ga_tracking">UA-xxxxx-x</twig:global>
    13. </twig:config>
    14. </container>
  • PHP

    1. // config/packages/twig.php
    2. $container->loadFromExtension('twig', [
    3. // ...
    4. 'globals' => [
    5. 'ga_tracking' => 'UA-xxxxx-x',
    6. ],
    7. ]);

Now, the variable ga_tracking is available in all Twig templates, so you can use it without having to pass it explicitly from the controller or service that renders the template:

  1. <p>The Google tracking code is: {{ ga_tracking }}</p>

Referencing Services

In addition to static values, Twig global variables can also reference services from the service container. The main drawback is that these services are not loaded lazily. In other words, as soon as Twig is loaded, your service is instantiated, even if you never use that global variable.

To define a service as a global Twig variable, prefix the service ID string with the @ character, which is the usual syntax to refer to services in container parameters:

  • YAML

    1. # config/packages/twig.yaml
    2. twig:
    3. # ...
    4. globals:
    5. # the value is the service's id
    6. uuid: '@App\Generator\UuidGenerator'
  • XML

    1. <!-- config/packages/twig.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:twig="http://symfony.com/schema/dic/twig"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/twig
    9. https://symfony.com/schema/dic/twig/twig-1.0.xsd">
    10. <twig:config>
    11. <!-- ... -->
    12. <twig:global key="uuid">@App\Generator\UuidGenerator</twig:global>
    13. </twig:config>
    14. </container>
  • PHP

    1. // config/packages/twig.php
    2. $container->loadFromExtension('twig', [
    3. // ...
    4. 'globals' => [
    5. 'uuid' => '@App\Generator\UuidGenerator',
    6. ],
    7. ]);

Now you can use the uuid variable in any Twig template to access to the UuidGenerator service:

  1. UUID: {{ uuid.generate }}

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.