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 }}
Related
In my template I need to print some items and add "," after each one, unless it is the last item.
So here is my code:
<li>
<strong>Category</strong>:
{% spaceless %}
{% for t in project.technology.all %}
{{ t.title }}
{% if not forloop.last %},{% endif %}
{% endfor %}
{% endspaceless %}
</li>
While it works, it adds an extra whitespace between each item and ",", so what I see is like this:
Tech1 , Tech2 , Tech3 while it should be Tech1, Tech2, Tech3
Even spaceless template tag is not working.
As the documentation of {% spaceless %} [Django-doc] says:
Only space between tags is removed – not space between tags and text.
It is thus only the space between tags, not between tags and text, text and text, etc.
You can render this with:
<li>
<strong>Category</strong>:
{% for t in project.technology.all %}
{{ t.title }}{% if not forloop.last %}, {% endif %}
{% endfor %}
</li>
we thus will not render a space between {{ t.title }} and the comma, since these are all template tags that do not output anything.
Lets say I have code in template like this:
<a href="#">
{% if request.user.first_name or request.user.last_name %}
{{ request.user.first_name }} {{ request.user.last_name }}
{% else %}
{{ request.user }}
{% endif %}
</a>
Problem with this code is that it adds trailing space to link, so link looks like link_ with underline at the end.
How do I remove such trailing spaces? {% spaceless %} tag doesn't quite help here because it only removes spaces between tags.
I actually found simple solution for my problem.
<a href="#">{% spaceless %}
{% if request.user.first_name or request.user.last_name %}
{{ request.user.first_name }} {{ request.user.last_name }}
{% else %}
{{ request.user }}
{% endif %}
{% endspaceless %}</a>
By placing spaceless tag inside it strips the string it gets. Placing outside
As a possible variant of decision:
http://www.soyoucode.com/2011/minify-html-output-django
Or you could try to create your own tag if there are no such tags:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
Quick workaround: Use html comments to "escape" the unnecessary whitespace.
Probably better solution: Create a template tag that holds this conditional.
Seconding the usage of a single template tag - it'd be good (and pretty easy) to remove this logic from the template.
Although, doesn't just using {{ request.user }} give exactly the same result as what you're doing here?
Instead of if-else block try to use shorter version:
{{ user.get_full_name|default:user.get_username }}
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
According to this section in the Django docs I should use {% blocktrans %} for cases where I need to translate pluralizations. However, with an example like the following, isn't there something more convenient I can do?
{% blocktrans count video.views.count as views %}
The video has been viewed <span>{{ views }}</span> time
{% plural %}
The video has been viewed <span>{{ views }}</span> times
{% endblocktrans %}
I tried to do the following:
{% blocktrans %}time{% plural %}times{% endblocktrans %}
But it threw TemplateSyntaxError: 'blocktrans' doesn't allow other block tags (seen u'plural') inside it
You forgot the count variable as variable_name in the blocktrans tag
The value of that variable will be used to detect if it's plural or not.
{% blocktrans count variable as variable_name %}
time
{% plural %}
{{ variable_name }} times
{% endblocktrans %}
You can use:
{% blocktrans with video.views.count|pluralize as foo and video.views.count as views %}
The video has been viewed <span>{{ views }}</span> time{{ foo }}
{% endblocktrans %}
I have a string that I want passed via the "linebreaks" filter.
{% trans "my string"|linebreaks %}
Doesn't work.
Is there another way ?
See filter.
{% filter force_escape|lower %}
{% blocktrans %}This text will be translated, HTML-escaped, and will appear in all lowercase.{% endblocktrans %}
{% endfilter %}
If you need to filter before translation, you can also use:
{% blocktrans with value|filter as myvar %}
This will have {{ myvar }} inside.
{% endblocktrans %}