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
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.
I'm altering an existing web interface to view ROBOT doc libraries, which uses a mixture of jinja (Python inside HTML) and HTML. I have never worked with jinja or HTML before and am having issues getting even a simple test case to work. When the browser loads the docs, I want our project's directory structure for the docs to be preserved to make finding things easier, and so I want to use jinja to create the dir structure. Here is a snippet of the code I'm working with:
{% extends "base.html" %}
{% block body %}
<div class="well" id="left">
<ul class="list-group list-unstyled">
{% set collection_list = [] %}
{% for collection in data.hierarchy %}
{% if collection.collection_id|string == data.collection_id|string %}
{% do collection_list.append(collection.path) %}
{% else %}
{% for link in collection.path_chain %}
<li>
<label class="tree-toggler nav-header"
title="file path: {{collection.path}}">{{link}}</label>
<ul class="list-group tree collapse"
id={{link}}>
</ul>
{% endfor %}
</li>
{% endif %}
...there's more after that, but this is where I hit the error. It sets the collection_list var fine, and the if statements work, but when it goes to execute the 'do' statement it fails with:
TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.
I don't believe this is an unclosed loop or something because if I replace the do statement with a simple test print statement, it works. Does anyone know what I'm doing wrong?
From the template documentation:
Expression Statement
If the expression-statement extension is loaded, a tag called do is available that works exactly like the regular variable expression ({{ ... }}); except it doesn’t print anything. This can be used to modify lists:
{% do navigation.append('a string') %}
You need to enable the Expression statement extension for this to work.
You didn't show how you load the Jinja2 environment, but loading extensions takes place via the extensions argument to the Environment() class:
jinja_env = Environment(extensions=['jinja2.ext.do'])
Is there any option to extend the tornado web template with the option inside the html template
{% code %}
# Python code
{% end %}
No, there is no such option. Tornado's templates are fairly liberal in allowing python code directly in the template, especially with the {% set %} directive (which can include any single-line statement, not just a variable assignment), but it is not possible to use multi-line statements except for the ones that have corresponding template directives ({% for %}, {% if %}, {% try %}, and {% while %})
I am using django to serve a page that includes a handlebars template. Mixing server and client side templating creates some ambiguity. If I have a template
<script id="my-script" type="text/x-handlebars-template">
<p> {{clientSideContent}} </p>
</script>
How can I tell the django templating engine that the {{clientSideContent}} tag isn't intended for it (since django and handlebars use the same tags)? Is there a raw strings tag in django, or an alternate good way to address this?
I think this should solve your problem
https://gist.github.com/ericflo/629508
so use something like
{% verbatim %} {{clientSideContent}} {% endverbatim %}
Never versions of django (starting from version 1.5) have support for the {% verbatim %} tag:
So something like this should work:
<script id="my-script" type="text/x-handlebars-template">
{% verbatim %}
<p> {{clientSideContent}} </p>
{% endverbatim %}
</script>
Hope it helps.
I'm trying to create a dynamic template from a string (I know, dynamic template is an oxymoron) but I'm having trouble with {% trans %} tags. If the string contains:
{% trans %}Hello{% endtrans %}
then when I do this:
context = RequestContext(request, data)
template = Template(text)
out_text = template.render(context)
I get an internal server error. I've tried adding {% load i18n %} to the string, but that doesn't help. What do I need to do to make the string translatable? It is working fine with html files, but I can't get it to work with strings. If I take the trans tags out, it parses the template string fine.
Are you sure this template is loaded and parsed by Jinja instead of Django?
I am using Jinja2 via Coffin in my Django project, and error you have always results when template is loaded without Coffin participation.
Check which loader found template first. Was it Coffin/Jinja one or Django loader? If it was found by Django, it will be parsed by Django, which doesnt know how to handle {% trans %}{% endtrans %} block.