Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Templating
  5. How to Inject Variables into all Templates (i.e. Global Variables)
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Using Service Container Parameters
  • Referencing Services
  • Using a Twig Extension

How to Inject Variables into all Templates (i.e. Global Variables)

Edit this page

Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to Inject Variables into all Templates (i.e. Global Variables)

Sometimes you want a variable to be accessible to all the templates you use. This is possible inside your app/config/config.yml file:

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
twig:
    # ...
    globals:
        ga_tracking: UA-xxxxx-x
1
2
3
4
5
<!-- app/config/config.xml -->
<twig:config ...>
    <!-- ... -->
    <twig:global key="ga_tracking">UA-xxxxx-x</twig:global>
</twig:config>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('twig', array(
     // ...
     'globals' => array(
         'ga_tracking' => 'UA-xxxxx-x',
     ),
));

Now, the variable ga_tracking is available in all Twig templates:

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

It's that easy!

Using Service Container Parameters

You can also take advantage of the built-in Service Container system, which lets you isolate or reuse the value:

1
2
3
# app/config/parameters.yml
parameters:
    ga_tracking: UA-xxxxx-x
  • YAML
  • XML
  • PHP
1
2
3
4
# app/config/config.yml
twig:
    globals:
        ga_tracking: "%ga_tracking%"
1
2
3
4
<!-- app/config/config.xml -->
<twig:config ...>
    <twig:global key="ga_tracking">%ga_tracking%</twig:global>
</twig:config>
1
2
3
4
5
6
// app/config/config.php
$container->loadFromExtension('twig', array(
     'globals' => array(
         'ga_tracking' => '%ga_tracking%',
     ),
));

The same variable is available exactly as before.

Referencing Services

Instead of using static values, you can also set the value to a service. Whenever the global variable is accessed in the template, the service will be requested from the service container and you get access to that object.

Note

The service is 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 string with @. This should feel familiar, as it's the same syntax you use in service configuration.

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
twig:
    # ...
    globals:
        user_management: "@acme_user.user_management"
1
2
3
4
5
<!-- app/config/config.xml -->
<twig:config ...>
    <!-- ... -->
    <twig:global key="user_management">@acme_user.user_management</twig:global>
</twig:config>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('twig', array(
     // ...
     'globals' => array(
         'user_management' => '@acme_user.user_management',
     ),
));

Using a Twig Extension

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 Extension and return the global variable as one of the entries in the getGlobals method.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Symfony Code Performance Profiling

Symfony Code Performance Profiling

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

Peruse our complete Symfony & PHP solutions catalog for your web development needs.

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Dragos Protung, a Symfony contributor

Thanks Dragos Protung (@dragosprotung) for being a Symfony contributor

4 commits • 6 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia