How to get config parameters in Symfony2 Twig Templates
Asked Answered
L

8

185

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.

Lupulin answered 22/7, 2011 at 9:12 Comment(0)
L
423

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');
Lachesis answered 2/12, 2011 at 17:20 Comment(3)
Good answer. Addendum: since Symfony 3.1 a variable must be quoted: "%app.version%"Bobble
@BigJ: Updated to include quotes, as this will also work in older versions.Lachesis
This should definitely be the accepted answer. Thanks !Capita
S
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.

Slowworm answered 22/7, 2011 at 19:40 Comment(1)
Better to set it in parameters like @Lachesis explained.Worl
M
93

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.

Mccurdy answered 28/3, 2012 at 13:58 Comment(1)
This looks like the best solution because it keeps all the knowledge of system together.Amati
W
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.

Warfourd answered 5/5, 2013 at 15:32 Comment(0)
V
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.

Vanatta answered 14/7, 2013 at 16:59 Comment(1)
Yeah nice, this answer very well explains both ways: passing via the controller and passing it as a global twig variable! +1Indophenol
K
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 }
Kaon answered 4/8, 2016 at 17:0 Comment(3)
Thanks for this, I was worried I had to duplicate the parameter from the parameters file to the twig globals.Banana
Nice, but it is not an unnecessary overload pull whole container to extension for one parameter?Salvatoresalvay
I believe with Symfony 2, you cannot inject container parameters (which is now possible with Symfony 4/5)Kaon
G
1

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

Grimaud answered 7/5, 2016 at 11:29 Comment(0)
I
0

In confing.yml

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

In Twig view

# twig view
{{ version }}
Irresponsible answered 21/4, 2017 at 7:46 Comment(2)
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 twigCobaltous
@PaulAndrieux just out of interest: why is this so bad if it is just done for some low-key variables?Sidewheel

© 2022 - 2024 — McMap. All rights reserved.