13

I have a field in a matrix block that is used for video embed codes. I want to output the contents of that field directly as HTML. However, it always seems to get entity-encoded (ie. the < and > get converted to &lt; and &gt;), so the html just appears as text.

I know about Twig's |raw filter, so I tried outputting the field's content through it (eg. using {{ element.blockContent|raw }}), but it seems to make no difference. Can anyone tell me what I'm doing wrong?

(Just in case it's relevant, this is a sub-template that is being included in a for loop that goes through the matrix blocks and outputs them)

2 Answers 2

12

The difference between "raw" and "escape" is sometimes confusing... Try this instead:

{{ element.blockContent|e }}


escape:

The escape filter escapes a string for safe insertion into the final output.


raw:

The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it.

1
  • Thanks for this. It turned out the problem for me was actually something else (see below), but this is still useful to know.
    – Nick F
    Aug 5, 2014 at 17:46
8

For what it's worth, the problem was due to a mistake on my part: the field in question had been set up as Rich Text, and then switched to Plain Text. I guess the original HTML had been encoded going into the database when the field was still Rich Text, and so was already entity-encoded before it even reached the template. When I pasted the HTML in question back in and saved the post again all was well.

So for me, the solution was: make sure that the HTML was definitely saved as raw HTML in a Plain Text field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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