Twig escaping html in if shorthand? - html

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?

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 %}

Related

Why does this jekyll template not work?

Short Version:
Why does the following code not produce an output when navbox.next_article is the string '2018-01-05-man-command'?!
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
Details
My post 2018-01-05-man-command.md has a YAML front matter:
---
layout : post
title : 'Man Command'
tags : [RHCSA, RHCSA_mod, Using Essential Tools, Man Command]
categories: [RHCSA]
navbox:
# prev_article:
next_article: 2018-01-05-understanding-globbing-and-wildcards
---
This is accessed by the _includes/post.html file through:
{% unless include.excerpt %}
{{ post.content }}
{% include navbox.html navbox=page.navbox %}
{% endunless %}
This is used by the _layout/post.html which sets the layout for the post:
{% include post.html post=page link_title=false %}
My navbox.html contains:
{% assign navbox = include.navbox %}
{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}
However, all I get when I run bundle exec jekyll serve is:
The next post is
Why does that line not work? I'm new to jekyll so it's possible I've made a blunder somewhere that's intuitive to most. Please tell me what I can fix.
I believe that the capture tag only captures strings, not posts. See here for more info.
I'm not convinced that a where filter supports the contains syntax you're using. See here for more info.
On top of that, where returns an array. You have to get the first item from that array.
You need to fix these issues. Use an assign instead of a capture to store a post. And change your where filter to not use the contains syntax, which isn't valid. (Unless it's been added since the issue I just linked.)
Here is how I've done it:
{% assign post = site.posts | where:"url", targetUrl | first %}

Liquid shorthands?

Are there any liquid shorthands? For example I'm trying to express this in a more concise way:
{% if job.offsite %}}
{{job.offsite}}
{{% else %}}
{{ job.url }}
{% endif %}
For this particular example, you could write:
{{ job.offsite | default: job.url }}
The value provided to the default filter will be used if the left side of the expression is false or nil.

Jekyll: Liquid filter "cgi_escape" returns error for some variables

The short version: have you managed to use something like {{ page.title | cgi_escape }} in an {% include %}-ed partial?
Details follow:
I have a partial that I use like so:
{% include mainContainer.html %}
Works fine. Then, in that partial, try to display some liquid variables:
{{ page.title }} displays the title.
{{ page.content }} Displays the content.
{{ page.content | cgi_escape }} displays the content, but escaped.
{{ page.title | cgi_escape }} doesn't work at all. Creates the following error:
Liquid Exception: undefined method `encoding' for nil:NilClass
Aside from {{ page.content }} I get the error for any of the {{ page }} variables (category, title, etc) but all of them will display fine without the filter. Also, {{ page.title | cgi_escape }} works okay in the...uhhh...'content' part of the layout (I'm not sure what to call it--the {{ content }} section). I only seem to get the error with {% include %}
Nevermind. The build was failing because a few of the pages using {% include mainContainer.html %} didn't have any front matter and thus no page.title (or whatever). Apparently, Liquid is willing to let {{ page.title }} pass if the variable isn't set, but not {{ page.title | cgi_encode }}

twig autoescaping html tags

{% autoescape false %}
{% set label = '<i class="flaticon solid plus-1"></i>Add User'|raw %}
{{ form_row(form.submit, { 'label': label }) }}
{% endautoescape %}
This is the output
<i class="flaticon solid plus-1"></i>Add User
How do I get it to not escape? If I just print out label instead of supplying it as a parameter to the "form_row" function, it prints out properly.
You're using the |raw filter at the wrong place - it is only processed when outputting data, not when setting a variable. You should patch it into the form_row function, or append it to it's call - can't be sure without seeing how that function works.
Most probably this will fix it:
{{ form_row(form.submit, { 'label': label })|raw }}
Since I assume it returns the modified string and lets the {{ tags handle output.
in form_div_layout.html.twig
change
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
to
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain)|raw }}</button>
This seems like a hacky solution, since I'm modifying the stuff in the vendor folder.

How to render links in my CharField/TextField DB column as HTML in Django

A snippet of my template -
{% block content %}
{{ message.subject }}
{{ message.content }}
{% endblock %}
My message.content = " Check this out - / RigWave "
If you want to render as HTML (as a link) like this -
" Check this out - RigWave "
After looking at your unedited post, I'm wondering if you replaced the link tags with [ to prevent SO from rendering it as a link (though `` takes care of it).
If you actually have properly formatted links in the CharField, you need to mark the string as safe to prevent auto HTML escaping.
{{ message.content|safe }}
or
{% autoescape off %}
{{ body }}
{% endautoescape %}
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters first sample is perfect, you could use custom filter or use .replace