187

I have a Symfony2 Twig template. I want to output the value of a config parameter in this twig template (a version number). Therefore I defined the config parameter like this:

parameters: app.version: 0.1.0 

I'm able to use this config parameter in Controllers but I have no clue how to get it in my Twig template.

8 Answers 8

424

You can use parameter substitution in the twig globals section of the config:

Parameter config:

parameters: app.version: 0.1.0 

Twig config:

twig: globals: version: '%app.version%' 

Twig template:

{{ version }} 

This method provides the benefit of allowing you to use the parameter in ContainerAware classes as well, using:

$container->getParameter('app.version'); 
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. Addendum: since Symfony 3.1 a variable must be quoted: "%app.version%"
@BigJ: Updated to include quotes, as this will also work in older versions.
This should definitely be the accepted answer. Thanks !
190

Easily, you can define in your config file:

twig: globals: version: "0.1.0" 

And access it in your template with

{{ version }} 

Otherwise it must be a way with an Twig extension to expose your parameters.

1 Comment

Better to set it in parameters like @Ryall explained.
94

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.

1 Comment

This looks like the best solution because it keeps all the knowledge of system together.
19

On newer versions of Symfony2 (using a parameters.yml instead of parameters.ini), you can store objects or arrays instead of key-value pairs, so you can manage your globals this way:

config.yml (edited only once):

# app/config/config.yml twig: globals: project: %project% 

parameters.yml:

# app/config/parameters.yml project: name: myproject.com version: 1.1.42 

And then in a twig file, you can use {{ project.version }} or {{ project.name }}.

Note: I personally dislike adding things to app, just because that's the Symfony's variable and I don't know what will be stored there in the future.

Comments

16

The above given ans are correct and works fine. I used in a different way.

config.yml

imports: - { resource: parameters.yml } - { resource: security.yml } - { resource: app.yml } - { resource: app_twig.yml } 

app.yml

parameters: app.version: 1.0.1 

app_twig.yml

twig: globals: version: %app.version% 

Inside controller:

$application_version = $this->container->getParameter('app.version'); // Here using app.yml 

Inside template/twig file:

Project version {{ version }}! {# Here using app_twig.yml content. #} {# Because in controller we used $application_version #} 

To use controller output:

Controller:

public function indexAction() { $application_version = $this->container->getParameter('app.version'); return array('app_version' => $application_version); } 

template/twig file :

Project version {{ app_version }} 

I mentioned the different for better understand.

1 Comment

Yeah nice, this answer very well explains both ways: passing via the controller and passing it as a global twig variable! +1
15

With a Twig extension, you can create a parameterTwig function:

{{ parameter('jira_host') }} 

TwigExtension.php:

class TwigExtension extends \Twig_Extension { public $container; public function getFunctions() { return [ new \Twig_SimpleFunction('parameter', function($name) { return $this->container->getParameter($name); }) ]; } /** * Returns the name of the extension. * * @return string The extension name */ public function getName() { return 'iz'; } } 

service.yml:

 iz.twig.extension: class: IzBundle\Services\TwigExtension properties: container: "@service_container" tags: - { name: twig.extension } 

3 Comments

Thanks for this, I was worried I had to duplicate the parameter from the parameters file to the twig globals.
Nice, but it is not an unnecessary overload pull whole container to extension for one parameter?
I believe with Symfony 2, you cannot inject container parameters (which is now possible with Symfony 4/5)
2

In confing.yml

# app/config/config.yml twig: globals: version: '%app.version%' 

In Twig view

# twig view {{ version }} 

3 Comments

giving access to the whole service container is not a good idea. the container is a service locator and it should not even be injected with dependency injection, so neither in twig
@PaulAndrieux just out of interest: why is this so bad if it is just done for some low-key variables?
previous answer injected whole service_container. This version injectig %app.version% feels ok
1

You can simply bind $this->getParameter('app.version') in controller to twig param and then render it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.