How to create a custom Twig function class, without using a static method?

Viewed 14423

I can create a Twig extension for my project like this

class Functions extends Twig_Extension{
    public function getName(){return 'foobar';}

    public function getFunctions() {
        return array(
            'loremipsum' => new \Twig_SimpleFunction('asset', 'Functions::loremipsum')
        );

    public static function loremipsum($foo) {
        return $foo;
    }
}

this works, but I want to use a constructor to inject some data that I need in some functions.

Simply using 'asset' in Twig_SimpleFunction will result in PHP trying to execute the functon loremipsum()

2 Answers
Related