Expressions, Control Flow & Filters
Two Syntaxes: Output vs. Statements
Jinja generates dynamic text (commonly HTML) by combining templates with data. {{ expression }} outputs a value into the rendered result; {% statement %} handles control flow, inheritance, and macros — a two-syntax split central to reading any Jinja template.
Loops, Conditionals & the loop Variable
<ul>
{% for item in items %}
<li>{{ loop.index }}. {{ item.name }}{% if not loop.last %},{% endif %}</li>
{% endfor %}
</ul>
{% if user.is_admin %}<a href="/admin">Admin</a>{% endif %}The implicit loop variable exposes iteration metadata (loop.index, loop.last) for numbering or separators without manual index tracking. {% set %} assigns a template-local variable computed once and reused later in the template.
Filters
{{ user.nickname | default('N/A') }}
{{ price | round(2) }}
{{ title | upper | truncate(30) }}Filters (piped with |) transform a value at render time — default provides a fallback for missing/falsy values, and multiple filters chain in sequence. Custom filters/globals registered from Python let an app expose domain-specific formatting to templates beyond the built-in set.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free