How to convert html tags from a database with twig?

Ask a question+

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);


And running this in twig:

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

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?

add comment

1 Answer


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