How to convert html tags from a database with twig?

Ask a question+

I am creating a new project in Slim 3. I have strings in a 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 make 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?

Arthur Dilanyan 530
add comment

1 Answer


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

Andranik Hovhannisyan 1.3K
add comment

Your Answer