How to get config parameters in Symfony2 Twig Templates
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.vers开发者_开发技巧ion: 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.
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');
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.
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.
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.
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.
With a Twig extension, you can create a parameter
Twig 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 }
You can simply bind $this->getParameter('app.version')
in controller to twig param and then render it.
In confing.yml
# app/config/config.yml
twig:
globals:
version: '%app.version%'
In Twig view
# twig view
{{ version }}
精彩评论