【发布时间】:2016-03-15 00:00:08
【问题描述】:
我正在开发第三方捆绑包。
我需要为这个包定义一个可以在树枝模板中使用的变量。
当我尝试在我的 bundle config.yml 模式中声明时,在我的项目中,变量在我的项目中步进 twig 模板,
twig:
globals:
test_vars: %test_vars%
我收到此错误。
InvalidArgumentException in YamlFileLoader.php line 357:
There is no extension able to load the configuration for "twig" (in /home/domain.ext/vendor/test/test-bundle/test/TestBundle/DependencyInjection/../Resources/config/.yml). Looked for namespace "twig", found none
非常感谢
解决方案代码,感谢@alexander.polomodov 和@mblaettermann
GlobalsExtension.php
namespace Vendor\MyBundle\Twig\Extension;
class GlobalsExtension extends \Twig_Extension {
public function __construct($parameter) {
$this->parameter= $parameter;
//...
}
public function getGlobals() {
return array(
'parameter' => $this->parameter
//...
);
}
public function getName() {
return 'MyBundle:GlobalsExtension';
}
}
my.yml
services:
twig.extension.globals_extension:
class: Vendor\MyBundle\Twig\Extension\GlobalsExtension
arguments: [%my.var%]
tags:
- { name: twig.extension }
my.html.twig
my parameter: {{ parameter }}
【问题讨论】:
-
您是否在 AppKernel 中包含了 TwigBundle 并在应用程序配置中设置了 twig?请参阅 github 上此文件的链接以了解 symfony 标准:github.com/symfony/symfony-standard/blob/2.8/app/config/…、github.com/symfony/symfony-standard/blob/2.8/app/…
-
感谢您的来信