Escape jinja2 syntax in a jinja2 template

FlaskJinja2

Flask Problem Overview


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?

Flask Solutions


Solution 1 - Flask

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJesvin JoseView Question on Stackoverflow
Solution 1 - FlaskMartijn PietersView Answer on Stackoverflow