how do I include quoted HTML in a Tornado Template? - templates

I'm using Tornado Templates and one of my fields is a string that has HTML tags quoted in it, e.g.
<p>Solar power</p>
When I render it into the template, the tags are quoted verbatim instead of treated as tags.
{{ quoted_html }}
So it looks exactly as above with the p tag visible.
In other templating systems, {{ = foo}} renders foo verbatim, but {{html foo}} treats the tags as tags.
Is there the equivalent in Tornado Templates?

{% raw foo %}, in Tornado 2.0+.
If you do that with a lot of expressions in a template, you can add the {% autoescape None %} directive to the beginning of the template, after which {{ foo }} will not be escaped.

Related

Escape jinja2 syntax in a jinja2 template

I serve dynamic pages from Jinja2 templates in Flask. Now I am defining client-side templates in say, Jinja2-clone Nunjucks inside a script tag. Problem is, the client-side templates has syntax like <% %> that Flask's Jinja2 interpreter may interpret instead of rendering verbatim.
How can I make the entire block of scripts render verbatim?
You can disable interpretation of tags inside a {% raw %} block:
{% raw %}
Anything in this block is treated as raw text,
including {{ curly braces }} and
{% other block-like syntax %}
{% endraw %}
See the Escaping section of the template documentation.

Django blocktrans - inside HTML tag

Is it possible to put some HTML tag inside this Django block: {% blocktrans %}{% endblocktrans %}?
For example:
{% blocktrans %}Django<br>framework needed{% endblocktrans %}
Certainly, sometimes you even have to use blocktrans template tag actually.
Check:
Translating text blocks with Django .. what to do with the HTML?
Django templates: Best practice for translating text block with HTML in it
https://groups.google.com/forum/#!topic/django-users/j_r6y1VeAag
From the docs:
HTML markup, however, is common enough that it's probably ok to use in translatable strings. But please bear in mind that the GNU
gettext tools don't verify that the translations are well-formed HTML.

_() or {% trans %} in Django templates?

In Django templates, you can use either {{ _("Hello World") }} or {% trans "Hello World" %} to mark strings to be translated. In docs, the “official” approach seems to be the {% trans %} tag, but the _() syntax is mentioned too once.
How these approaches differ (except syntax) and why should be one preferable rather than the other?
One difference is that you obviously can't use {% trans %} with tags and filters. But does that mean that I can just use _() everywhere, like {{ _("String") }}? It works and looks much cleaner and more consistent than using {% trans "String" %} with standalone strings and _() with tags and filters.
So it seems that there's technically no difference as of Django 1.5. Template engine internally marks a variable for translation (by setting its translate attribute) in two cases:
when you do {% trans VAR %} (see TranslateNode), or
if the name of a variable starts with _( and ends with ) (see Variable.__init__).
Later, when the variable is being resolved, Django wraps it with ugettext or pgettext if it sees the translate attribute.
However, as can be seen from source code, there are some flexibility considerations in favor of {% trans %} tag:
you can do {% trans "String" noop %}, which will put the string for translation into .po files, but won't actually translate the output when rendering (no internal translate attribute on variable, no ugettext call);
you can specify message context*, like {% trans "May" context "verb" %};
you can put translated message into a variable for later use*, like {% trans "String" as
translated_string %}.
* As of Django 1.4.
Please feel free to correct me or post a better answer in case I'm missing anything.
The trans template tag calls the ugettext() function. In Django _() is an alias to ugettext().
This is covered in the django docs.

How to filter a template tag?

I've got a tag that looks like this:
{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}
Which just renders an empty form. But now I want to pass the output of that to the escapejs filter so that I can use it in a JavaScript variable. How can I do that?
Many tags support as variablename -- that is, simple put as variablename at the end of the tag and then the output of that tag is placed in the variable rather than displayed.
This {% partial %} tag may support that. Here's an example, if it does:
{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form as myvar %}{{ myvar|escapejs }}
If the tag in question is the "Partial tag" snippet then it appears it doesn't support this. But it probably could be rewritten to support it.
You could use the "Capture template output as a variable" snippet, and then apply the filter to the captured content, like so:
{% captureas myvar %}{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}{% endcaptureas %}{{ myvar|escapejs }}
Applying a filter to the output of a template tag can also be accomplished without any external dependencies using the built in filter template tag. From the documentation:
[This template tag] filters the contents of the block through one or more filters. Multiple filters can be specified with pipes and filters can have arguments, just as in variable syntax.
The example in the original question would be written like this:
{% filter escapejs %}
{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}
{% endfilter %}
Another solution to get the data into a JS variable:
<div class="display:none" id="empty-vehicle-form">{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}</div>
Then nab it and remove it at the same time
var empty_form = $('#empty-vehicle-form').remove().html();
The advantage of this solution is that your other JS scripts can preprocess it before you rip it out of the DOM. escapejs also creates bigger filesizes with all those escape chars.

How do you print out the exact text "{{text}}" in a Django template?

How do you print out "{{text}}" in a Django template? If I type it into a Django html template it gets interpreted as the variable text. I want the actual text:
{{text}}
To appear in the html output.
To output the characters used to compose template tags, you have to use a specific template tag called templatetag. If you want to output the {{ characters, for example, you use {% openvariable %} and the output of that template tag would be {{.
So for your example,
{% openvariable %} text {% closevariable %}
would output:
{{ text }}
The best way is to use the templatetag tag. However, if i recall correctly, using {{ "{{text}}" }} will also work, this is however undocumented behaviour, so there is no real guarantee this will never break.