How to escape liquid template tags? - templates

This sounds very easy, however I couldn't find it anywhere in the docs. How can I write {% this %} in a liquid template, without it being processed by the engine?

it is possible to disable liquid processing engine using the raw tag:
{% raw %}
{% this %}
{% endraw %}
will display
{% this %}

For future searchers, there is a way to escape without plugins, use the code below:
{{ "{% this " }}%}
and for tags, to escape {{ this }} use:
{{ "{{ this " }}}}
There is also a jekyll plugin for this which makes it a whole lot easier: https://gist.github.com/1020852
Raw tag for jekyll. Keeps liquid from parsing text betweeen {% raw %}
and {% endraw %}
Reference

BTW:
If you want to display {{ "{% this " }}%}in Jekyll, you can code like this:
{{ "{{ " }}"{{ "{% this" }} " }}{{ "}}%}
To escape {{ "{{ this " }}}}use:
{{ "{{ " }}"{{ "{{ this" }} " }}{{ "}}}}

You can escape liquid tags in Jekyll posts using {% raw %} {% endraw %} i.e
{% raw %}
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
{% endraw %}
will produce
{% for post in site.posts %}
{{ post.content }}
{% endfor %}

There is another option: to use HTML special characters codes for replacing the curly braces with its matching codes:
replace each { with {
replace each } with }
For more details about this solution see:
http://www.tikalk.com/devops/curly_brances_workaround/

I found a omnipotent way to display any text with curly braces. You can assign plain text to a variable, and display it.
{% assign var = "{{ sth }}" %}
{{ var }}

As mentioned here also, plain {% raw %} and {% endraw %} are only the second best solution since those are shown if you look up the Markdown on normal github.com.
The best way is to put {% raw %} and {% endraw %} in HTML comments:
<!-- {% raw %} -->
something with curlky brackets like { this } and { that }
<!-- {% endraw %} -->
Due to the HTML comments it is seen by Github as a comment. In Github pages the raw tags will prevent the parsing of the curly brackets in between the tags.

I tried {% raw %} something {% endraw %} ,
and {{ "{% this " }}%}. But they both don't work.
finally, my working answer is
{{ "{%" xxx }} something }}.
My code:
{{ "{%" }} extends 'xadmin/base_site.html' %}
{{ "{%" }} block nav_form %}
<h3>{{ "{{" }} title }}</h3>
{{ "{%" }} for i in context1 %}
<p>{{ "{{" }} i }}</p>
{{ "{%" }} endfor %}
{{ "{%" }} endblock %}
The result:
{% extends 'xadmin/base_site.html' %}
{% block nav_form %}
<h3>{{ title }}</h3>
{% for i in context1 %}
<p>{{ i }}</p>
{% endfor %}
{% endblock %}

Allows output of Liquid code on a page without being parsed.
{% raw %}{{ 5 | plus: 6 }}{% endraw %} equals 11.
{{ 5 | plus: 6 }} equals 11.
For more details about this solution see: https://www.shoplazza.dev/docs/theme-tags

Related

How to display text if the field is empty?

How to display a text if the field is empty ?
I tried the following code but it does not work :
{% if content.field_description is not empty %}
{{ content.field_description }}
{% else %}
test
{% endif %}
If you have Twig Tweak installed, you can do the following:
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
When field is empty it is not coming with content variable
so you can simply check by isset
{% if content.field_description %}
{{ content.field_description }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
The below worked for me:
{% if content['field_description'] IS NOT EMPTY %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
Please note that it may vary based on the content type and fields you are dealing with.

Separator Between Multiple Variables in Django Template (DTL)

Given three or more variables in my DTL template what is the most convenient way to assure that an interpunct is always between two variables?
<div>{{ person.name }} · {{ person.phone }} · {{ person.city }}</div>
Expected:
<div>John · 1234567 · New York</div>
<!-- {{ person.city }} is null or empty-->
<div>John · 1234567</div>
Is there an easy way to solve this with built-in functionality? I try to avoid writing custom Django filters/template tags.
You can't do this easily in Django template language. Either pass a list of non-empty values from your view, e.g. ["John", "1234567"], or write a custom tag or filter so that you can do something like {% interpunct person.name person.phone person.city %} or {{ person|display_person }}.
You can (ab)use the cycle tag for this purpose. Place it in front of each list item, then use it to emit nothing on first pass, then the separator thereafter. Use in a for loop is straightforward. Use outside needs this pattern:
{% cycle '' '|' '|' '|' as itemsep silent %}
{% if foo %}
{{ itemsep }}{% cycle itemsep %}
Foo!
{% endif %}
{% if bar %}
{{ itemsep }}{% cycle itemsep %}
Bar!
{% endif %}
{% if foobar %}
{{ itemsep }}{% cycle itemsep %}
FooBar!
{% endif %}
Or, miss the silent complexity by just using an extra ''
{% cycle '' '' '|' '|' '|' as itemsep %}
{% if foo %}
{% cycle itemsep %}
Foo!
{% endif %}
...

Using braces in blocktrans

How can I escape {{ or }} inside a blocktrans tag?
Solutions like {% verbatim %) or {% templatetag openvariable %} are not allowed inside a blocktrans tag.
I think you can do it like this:
{% blocktrans with verbatim="your verbatim {{ text }}" %}
Some stuff including {{ verbatim }}.
{% endblocktrans %}
This will preserve the braces around {{ text }}

How can i check if values of models are the same in django loop?

I've a loop:
{% for addimg in post.addimg_set.all %}
<p>
{{ addimg.execution }}<br>
{{ addimg.width }} cm x {{ addimg.height }} cm<br>
{{ addimg.year }}
</p>
{% endfor %}
I want to display some data as long as they are different. If they are equal i want it only to display once like:
{% if addimg.execution == addimg.execution %}
{{ addimg.execution }}<br>
{% endif %}
forgive me my python/django still young.
Any suggestions?
You may find your answer in documentation.
Check this out:
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#ifchanged
I think you are looking for the {% ifchanged %} template tag.
{% ifchanged addimg.execution %}
{{ addimg.execution }}<br>
{% endifchanged %}

Customizing a Django admin template

I would like to modify an admin template in Django.
% cat /Library/Python/2.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}<div class="description">{{ fieldset.description|safe }}</div>{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} ">
{{ line.errors }}
{% for field in line %}
<div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}{{ field.field }}
{% endif %}
{% if field.field.field.help_text %}<p class="help">{{ field.field.field.help_text|safe }}</p>{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
What kind of object is field, and more specifically how would I get the name of a field?
field is an instance of AdminField and field.field is an instance of BoundField, so you can reference the fields name with:
{{ field.field.name }}
Once as you start to dive deep into admin customisation, its the only place the documentation is really lacking. that being said, the code is well written and easy to understand if you take the time to research it, IMHO.
There is not many files so take an evening and read through them. In your case, I would start with:
contrib/admin/sites.py
contrib/admin/options.py
contrib/admin/helpers.py
Have you done your research?
After that, I would start poring over the python code that invokes your template. I would imagine that field is from the forms system
Field
A class that is responsible for doing validation, e.g. an EmailField
that makes sure its data is a valid
e-mail address.