Score:-1

HTML not rendered in text field content type

cn flag

In my content type, I have a text field that will display a string (max 200 characters) in a Bootstrap card. Due to the design, we have to allow the user to add a < br /> or \ n to add "invisible lines" to ensure content is wrapped correctly in the card.

By adding "< br /> to the field however, in twig, the < br /> is printed to screen and not HTML rendered. I have tried:

<p>{{content.field_copy}}</p>    
<p>{{content.field_copy|raw}}</p>
<p>{{content.field_copy|render}}</p>

With no affect. What other options do I have? Can this be done using a text field?

4uk4 avatar
cn flag
For this use case Drupal has an entire ecosystem with formatted text fields, text formats and text filters. Don't try to do this on your own. There are a lot of security considerations.
Score:0
cn flag

it's bad practice, but still

{{ content.field_copy.0['#context'].value|raw }}
4uk4 avatar
cn flag
This is not only bad practice, this is extremely dangerous. Drupal stores user input as it is in the database and the security of a Drupal site relies on that it is not displayed without sanitizing.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.