Jinja2 (Flask)

Jinja2 is the templating engine bundled with Flask. It renders HTML files that mix static markup with placeholders like {{ variable }} and logic blocks like {% for %}, filling them with data you pass from your views.

Flask

Jinja2 separates presentation from logic. Your view computes data and passes it to a template; the template decides how to display it. Expressions go in {{ }}, statements (loops, conditionals, blocks) go in {% %}, and comments go in {# #}.

Powerful features include template inheritance ({% extends %} / {% block %}), filters that transform values ({{ name|upper }}), macros for reusable snippets, and auto-escaping that protects against XSS by escaping HTML in variables by default.

Flask configures Jinja2 to look in your templates/ folder and injects helpers like url_for and get_flashed_messages into every template's context automatically.

Syntax

{{ value }}   {% for x in items %}...{% endfor %}

Examples

Python — Rendered with render_template('list.html', items=['a','b'])

<!-- templates/list.html -->
<ul>
{% for item in items %}
  <li>{{ item|capitalize }}</li>
{% endfor %}
</ul>

Learn it in a lesson

See also

Back to the Dictionary