如何告诉 Symfony2 不要app在模板中设置全局变量(如)?我想设置自己的app变量,但Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables Object如果我在控制器中覆盖它,它就会有类型事件。
1 回答
1
我认为这是这样的可能性(基本上从来没有因为appvar 可以派上用场;))
appglobal 被添加到Symfony\Bundle\TwigBundle\TwigEngine(for twig)的构造函数和Symfony\Bundle\FrameworkBundle\Templating\PhpEngine(for php)的构造函数中
但是当你不传递GlobalVariables给构造函数(可以为空)时,它不会设置app变量。所以你可以像这样覆盖templating服务:
<service id="templating" class="Acme\DemoBundle\Templating\TwigEngine">
<argument type="service" id="twig" />
<argument type="service" id="templating.name_parser" />
<argument type="service" id="templating.locator" />
</service>
和 php 文件:
<?php
namespace Acme\DemoBundle\Templating;
use Symfony\Bundle\TwigBundle\TwigEngine as BaseEngine;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Config\FileLocatorInterface;
class TwigEngine extends BaseEngine
{
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator)
{
parent::__construct($environment, $parser, $locator);
}
}
您应该知道,您也可以在没有它的情况下实现自己的全局变量......只需定义自己的templating.globals服务来替换原来的服务。
于 2013-02-06T10:43:13.853 回答