Twig string render and Symfony extensions

This method works without cloning the twig environment: (Tested in symfony 3)

$rendered = $this->get('twig')
    ->createTemplate('Hi {{ name }}!')
    ->render(['name' => 'simon']);

Symfony extensions like path work, like requested.


Try this :

$twig = clone $this->get('twig');
$twig->setLoader(new \Twig_Loader_String());
$rendered = $twig->render(
    "Test string template: {{ result|humanize }}",
    array("result" => "mega_success")
);

cf. How to render a string as a Twig template in Symfony2

Tags:

Twig

Symfony