N
N
Nikita Kolesnikov2019-02-24 14:26:41
PHP
Nikita Kolesnikov, 2019-02-24 14:26:41

Display problem in twig?

Hi!
I ran into a problem
I did some localization on the site, localization phrases are stored in the database, they are available on demand.
Phrases can contain html and some variables that are passed to the template.
I figured out the variables, I was able to shove them in, but what about html?
html is processed if the phrase is output via echo, but then the replace tweak (to substitute variables) does not work, but if the phrase is shoved through return, it’s the other way around: there is no html processing, but there are variables
And if you use return, it’s the other way around ..
Pieces of code and examples here: https://codeshare.io/21yd3B

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Kolesnikov, 2019-08-13
@NikitOS_MV


The answer was in a conspicuous place, but I blunted it, and after a couple of days I found the answer, but I remembered the question only now
That is, it is solved by adding a raw filter at the end

Y
Yuri Esin, 2019-02-24
@Exomode

1) Write a variable substitution function for a Twig extension:

$prepareString = new Twig_SimpleFunction('prepareString', function ($str) {
  return str_replace("%param1%", "Тест", $str);
});

2) Add it to extensions:
3) In the template, call the function where necessary:
You can write all the variable replacement logic you need in $prepareString. On regular expressions, on enumeration of the dictionary, through str_replace as in my example, or in any other way. You pre-form the data for substitution from the database or in any other way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question