Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Updating link to the docs
Source Link
Francesco Casula
  • 27.3k
  • 16
  • 139
  • 136

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.

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

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

added 2 characters in body
Source Link
Francesco Casula
  • 27.3k
  • 16
  • 139
  • 136

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

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

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

update according the official doc
Source Link
Francesco Casula
  • 27.3k
  • 16
  • 139
  • 136

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

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.html

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

improved answer
Source Link
Francesco Casula
  • 27.3k
  • 16
  • 139
  • 136
Loading
Source Link
Francesco Casula
  • 27.3k
  • 16
  • 139
  • 136
Loading