Stefano Stefano - 1 year ago 91
PHP Question

Render template from twig extension

I have built a twig extension to do some things and one of them is render a template. How can I access from inside the twig extension the engine environment and call the Render method?

Answer Source

You can define the extension so that it needs the environment. Twig will automatically pass it to the function.

public function getFunctions()
{
    return array(
        'myfunction' => new \Twig_SimpleFunction(
            $this,
            'myFunction',
            array('needs_environment' => true)
        ),
    );
}

public function myFunction(\Twig_Environment $environment, string $someParam)
{
    // ...
}