How to convert html tags from a database with twig?

Ask a question
1

I creating a new project in Slim 3. I have strings in database (mysql) which have html tags, like:

<b>My</b> first project <b>in</b> March

And i have a question how i can convert this specific tags: to be real tags and bold my text in html?

I make function in Slim

$function = new Twig_SimpleFunction('convertToHtml', function ($string) {
    return htmlspecialchars_decode($string);
});

$container->get('view')->getEnvironment()->addFunction($function);

And running this in twig:

  <div class="uk-position-center uk-light">
     <h4 class="uk-margin-remove">{{ convertToHtml(data.project_name) }}</h4>
  </div>

And it’s showing strings with tags, not bold. In fact, when i makes it in clear php it’s work. I take strings from db and convert it with htmlspecialchars_decode function - it’s work.

So, how i can repair this?

offer bounty
add comment

1 Answer

1

Arthur you can use twig’s raw filter to disable escaping. Something like this: {{ data.project_name|raw }}
Fore more details see twig docs

add comment

Your Answer