php - Twig dynamic functions
260
The Twig documentation notes the following approach for creating a dynamic function:
$twig->addFunction('*_path', new Twig_Function_Function('twig_path'));
function twig_path($name, $arguments)
{
// ...
}
Since I already encapsulate this code within a function, I'd like to avoid creating a function within a function. How can I place 'function twig_path' outside of this scope and still load it?
Answer
Solution:
The best way is to encapsulate your extension in a class, then use
Twig_Function_Method
instead ofTwig_Function_Function
. For an example see MarketplaceExtension.