Answer the question
In order to leave comments, you need to log in
twig. How to substitute a variable in the text from the database?
Hi!
How to solve this...
Phrases are stored in the database, they are called by a function in twig on the site
Let's say there is a phrase: Hello, XXX. Instead of XXX, you need to insert a variable that is passed as the second parameter to the function, but how?
If the phrase is stored in this form: Hello, {{ name }}, then when the name variable is passed to the template, nothing changes and the output is the same "Hello, {{ name }}"
Answer the question
In order to leave comments, you need to log in
Perhaps the replace filter will help you.
https://twig.symfony.com/doc/2.x/filters/replace.html
$loader = new Twig_Loader_Array([
'index.html' => 'Hello {{ name }}!',
'page.html' => 'Page {{ name }}! {{ content }}',
]);
$twig = new Twig_Environment($loader);
echo $twig->render('index.html', ['name' => 'Fabien']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question