I try to get access to a Twig Extention function I have written.
// 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';
}
}
Now I try to access the funtion by {{ testMethod() }}
, but I get the following error:
UndefinedFunctionException in <Hex for cached view>.php line 68: Attempted to call function "testMethod" from the global namespace.
I cleared the cache and tried to search for the error, but I found nothing that helped me. Maybe here can someone help.
You are defining your Twig_Function
wrong, as it stands now, you told Twig
to look for a global function
, defined outside any class.
If you want to tell Twig
to look inside the current class, you can do this with:
public function getFunctions() {
return [
new \Twig_SimpleFunction('testMethod', array($this, 'testMethod')),
];
}