I know that the controller can retrieve the value set to parameters in config.yml by $this->container->getParameter(), but is it not possible to retrieve it directly from the template?
For the symfony.1 series, I think I could get it through sfConfig::get('sf_category') etc., but if you can do something similar, please tell me how to do it.
php symfony2
If you want to get parameters from the template (Twig), you must register it in the global variable of twig yourself.
It's in English, but this area is helpful.
http://symfony.com/doc/current/cookbook/templating/global_variables.html
Or create your own TwigExtension
http://docs.symfony.gr.jp/symfony2/cookbook/templating/twig_extension.html
Another method is to implement the getGlobals() method.
http://twig.sensiolabs.org/doc/advanced.html#id1
If it is troublesome to set up parameters one by one, I think it would be better to implement them as follows.
ex.)
services.yml
services:
app.twig_extension:
class:AppBundle\Twig\AppExtension
public —false
arguments:
- "@=service('service_container').parameters().all()"
tags:
- {name:twig.extension}
Create twig extension
class AppExtension extensions\Twig_Extension
{
private$params;
public function__construct(array$params)
{
$this->params=$params;
}
public function getGlobals()
{
return ['params' =>$this->params];
}
}
How to Use Twig
{{params.myParam}}
© 2024 OneMinuteCode. All rights reserved.