N
N
Nikita Kolesnikov2019-02-03 18:41:54
PHP
Nikita Kolesnikov, 2019-02-03 18:41:54

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

2 answer(s)
V
Vadim, 2019-02-03
@NikitOS_MV

Perhaps the replace filter will help you.
https://twig.symfony.com/doc/2.x/filters/replace.html

A
Anton Shamanov, 2019-02-03
@SilenceOfWinter

$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 question

Ask a Question

731 491 924 answers to any question