Answer the question
In order to leave comments, you need to log in
How to properly render a template from a string?
Greetings!
There is a line containing a Twig template:
$template = 'Hello {{ name|capitalize }}!';
And there is an array of data to substitute into the template
$data = ['name' => 'world']
How to get the template rendered by twig into a variable?
Answer the question
In order to leave comments, you need to log in
$loader = new \Twig\Loader\ArrayLoader([
'you_name.html' => 'Hello {{ name }}!',
]);
$twig = new \Twig\Environment($loader);
echo $twig->render('you_name.html', ['name' => 'Fabien']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question