Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape jinja2 syntax in a jinja2 template

Tags:

flask

jinja2

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?

like image 963
Jesvin Jose Avatar asked Aug 18 '14 09:08

Jesvin Jose


People also ask

What is Jinja2 syntax?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.


1 Answers

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.

like image 181
Martijn Pieters Avatar answered Oct 06 '22 13:10

Martijn Pieters