我尝试访问我编写的Twig Extention函数。
// AppBundle/Twig/AppExtention.php
namespace AppBundle\Twig;
class AppExtension extends \Twig_Extension
{
public function getFunctions() {
return [
new \Twig_Function('testMethod', 'testMethod'),
];
}
public function testMethod() {
return 'blubb';
}
}
现在我尝试通过{{ testMethod() }}
访问功能,但是我收到以下错误:
UndefinedFunctionException in <Hex for cached view>.php line 68: Attempted to call function "testMethod" from the global namespace.
我清除了缓存并尝试搜索错误,但我发现没有任何帮助我。也许这里可以有人帮忙。
答案 0 :(得分:1)
您正在定义Twig_Function
错误,就像现在一样,您告诉Twig
寻找在任何类别之外定义的global function
。
如果您想告诉Twig
查看当前班级,可以执行以下操作:
public function getFunctions() {
return [
new \Twig_SimpleFunction('testMethod', array($this, 'testMethod')),
];
}