php - Twig dynamic functions


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?




The best way is to encapsulate your extension in a class, then useTwig_Function_Method instead ofTwig_Function_Function. For an example see MarketplaceExtension.

People are also looking for solutions to the problem: java - Error converting a HTTP POST response to JSON


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.