我试图访问我编写的Twig扩展函数。
// 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.
我清除了缓存,并试图搜索错误,但我没有发现任何帮助我。也许这里有人能帮忙。
发布于 2017-04-11 13:53:59
您正在错误地定义您的Twig_Function,现在,您告诉Twig寻找一个在任何类之外定义的global function。
如果您想让Twig查看当前类中的内容,可以使用以下方法完成:
public function getFunctions() {
return [
new \Twig_SimpleFunction('testMethod', array($this, 'testMethod')),
];
}https://stackoverflow.com/questions/43347634
复制相似问题