0

I have a variable in my twig template called description.
The variable is a string with an html em tag in it. When I go to the webpage to view the field, the html em tags are stripped out of the string.
I am on a Drupal 8 site and my variable was rendered through
\Drupal::service('renderer')->render() The twig autoescape parameter is set to false.

Using the twig debugger, I can see that my markup object includes the html em tags so I know that the problem is how twig is processing the string.
I've read in other posts to use the |raw tag but this has no effect.
What can I do to make the HTML in my twig variable render properly?

Update: I've included a screenshot of my output from {{ dump(description) }} enter image description here

  • 1
    can you share the line you have used into your twig file to print that variable? – MiharbKH Apr 11 at 14:29
  • {% if description is not empty %} <p>{{ description }}</p> {% endif %} – Trey D Apr 11 at 14:29
  • try to allow uses of <em> tag {{ description|striptags('<em>')|raw }} – MiharbKH Apr 11 at 14:36
  • Just tried this but no difference. I know that my variable has the html em tags because I can see them when I run dump( description ). – Trey D Apr 11 at 14:42
  • can you share a screen of your dump results? try to update the post by adding your screenshot. – MiharbKH Apr 11 at 14:44
3

my variable was rendered through \Drupal::service('renderer')->render()

Don't do that - Drupal sets Twig up to understand render arrays. Just use:

{{ original_description_render_array }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.