Stefano Stefano - 10 months ago 76
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(
            array('needs_environment' => true)

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