.. index::
single: Templating; Global variables
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:
.. configuration-block::
.. code-block:: yaml
# config/packages/twig.yaml
twig:
# ...
globals:
ga_tracking: 'UA-xxxxx-x'
.. code-block:: xml
The Google tracking code is: {{ ga_tracking }}
Referencing Services -------------------- In addition to static values, Twig global variables can also reference services from the :doc:`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 :ref:`refer to services in container parameters