Reputation: 399
I have a piece of template as follows:
<div class="reply_text"><div class="wall_reply_text">
{{ reply.comment_text is empty ? '<div class="has-text-grey-light">This comment is empty.</div>' : reply.comment_text }}
</div></div>
Instead of showing This comment is empty.
using the class has-text-grey-light
, it shows literally this unescaped text: <div class="has-text-grey-light">This comment is empty.</div>
.
I believe that previously I already made similar things and it did output correctly. What's wrong here?
Upvotes: 0
Views: 252
Reputation: 7654
Try 'raw' filter.
{{ var|raw }} {# var won't be escaped #}
{{ (true ? '<b>Hello 1</b>' : '<p>Hello 2</p>')|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:
<div class="reply_text">
<div class="wall_reply_text">
{{ (reply.comment_text is empty ? '<div class="has-text-grey-light">This comment is empty.</div>' : reply.comment_text) | raw }}
</div>
</div>
Another way is using if else statement where you don't need to use the raw filter.
{% if reply.comment_text is empty %}
<div class="has-text-grey-light">This comment is empty.</div>
{% else %}
{{ reply.comment_text }} {# if required {{ reply.comment_text | raw }} #}
{% endif %}
Upvotes: 1