Escape jinja2 syntax in a jinja2 template - flask

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.

Related

Syntax to supply a parameter to a custom template tag in {% if %} block

I have setup a custom template tag (simple_tag) (using https://stackoverflow.com/a/7716141/1369798) with a definition like this:
templatetags/polls_extras.py
def settings_value(name)
which I am able to use in my template like this:
templates/index.html
{% settings_value "ALLOWED_BOOL" %}
But this just inserts the text into my HTML output.
What is the syntax to use my template tag with parameter in an {% if %}?
I tried this but I get the error: TemplateSyntaxError at / Unused '"ALLOWED_BOOL"' at end of if expression.
templates/index.html
{% if settings_value ALLOWED_BOOL %}
You are allowed.
{% endif %}
You cannot use a templatetag as a parameter to another templatetag. Your options here are either
modifying your settings_value templatetag so it can inject the value in the current context, ie :
{% settings_value ALLOWED_BOOL as allowed_bool %}
{% if allowed_bool %}
You are allowed.
{% endif %}
Note that simple_tag won't work here, you'll either have to switch to assignement_tag (if your Django version support it) - but then you'll loose the ability to directly output a setting in the template the way you actually do - or write a full blown custom templatetag (which is not as difficult as it might seem at first).
Use a custom context_processor instead like danhip suggests - but then only templates rendered using a RequestContext will access these variables.

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.

how do I include quoted HTML in a Tornado Template?

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.

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.

Writing eclipse templates

I am writing django templates in Eclipse->prefrences->templates, to autocomplete DJango templates. I wrote this
{% block ${cursor} %}
{% endblock %}
Now, when I request and do autocompletion, after typing {% the autocompletion is
{% {% block %}
{% endblock %}
While I would like
{% block %}
{% endblock %}
With cursor after block. How can I do this?
Instead of typing {% and selecting dj_for_empty, try typing dj_ and then auto-completing. It will behave the way you expect in that case.
BOTTOM-LINE: You auto-complete the templates into the editor based on the template name, not based on the template contents.
It appears that autocompletion has two sources: regular HTML tags (for which I can't find the definitions to change anywhere in Eclipse, sorry) and the templates themselves (which you correctly demonstrated in your comment with the screenshot).
Look at this image:
Instead of typing <t and triggering auto-complete, I typed t. You can see that there are entries with <> - indicating these are autocompletions based on the actual HTML tag - and entries with # - indicating these are autocompletions based on a template.
It appears templates are to be accessed by the name of the template. Notice that the template named table provides a complete <table> and not just the <table></table> that is autocompleted if you just type <tab and autocompletes.