Jinja Template Builder
Build configuration templates with Jinja2 syntax highlighting
Quick Reference
Common Jinja2 syntax patterns and their usage
{% for item in list %}
Loop over items in a sequence
{% if condition %}
Conditional statement
{{ variable }}
Output a variable value
{{ var | filter }}
Apply a filter to value
{% set x = value %}
Assign a variable
{% include 'file' %}
Include another template
{% macro name() %}
Define a reusable macro
{{ loop.index }}
Current loop iteration (1-based)
Complete Reference Guide
Template Delimiters
{{ expression }}
Output a value. Use for variables, calculations, function calls.
{% statement %}
Control logic. Use for loops, conditions, assignments, blocks.
{# comment #}
Comments. Not rendered in output.
Variable Access
{{ foo.bar }}
Dot notation - checks attribute first, then item.
{{ foo['bar'] }}
Subscript notation - checks item first, then attribute.
{{ list[0] }}
Access list/array by index (0-based).
Expressions
{{ 'yes' if active else 'no' }}
Inline conditional (ternary). Returns first value if true, second if false.
{{ "Hello " ~ name }}
String concatenation using tilde (~) operator.
{{ items | join(', ') }}
Filter syntax - pipe passes value to filter function.
For Loops
{% for item in items %}...{% endfor %}
Basic iteration over a sequence.
{% for key, value in dict.items() %}...{% endfor %}
Iterate over dictionary key-value pairs.
{% for item in items if item.active %}...{% endfor %}
Loop with inline filter - only iterate matching items.
{% for item in items %}...{% else %}No items{% endfor %}
Else block runs when sequence is empty.
Loop Variables
| Variable | Description |
|---|---|
loop.index | Current iteration (1-indexed) |
loop.index0 | Current iteration (0-indexed) |
loop.first | True if first iteration |
loop.last | True if last iteration |
loop.length | Total number of items |
loop.cycle('a','b') | Cycle through values each iteration |
loop.previtem | Previous item (undefined on first) |
loop.nextitem | Next item (undefined on last) |
Conditionals
{% if condition %}...{% endif %}
Basic conditional block.
{% if x %}...{% elif y %}...{% else %}...{% endif %}
Full conditional with elif and else branches.
{% if x and y %}
Both conditions must be true.
{% if x or y %}
Either condition can be true.
{% if not x %}
Negation - true when x is false/empty.
{% if 'admin' in roles %}
Membership test - check if value in list.
Assignment
{% set name = value %}
Create or update a variable.
{% set a, b = func() %}
Unpack multiple return values.
Text Filters
| Filter | Description |
|---|---|
upper | Convert to uppercase |
lower | Convert to lowercase |
capitalize | Capitalize first character |
title | Title Case Each Word |
trim | Remove leading/trailing whitespace |
replace('old','new') | Replace substring |
truncate(50) | Truncate to length with ellipsis |
wordwrap(80) | Wrap text at specified width |
indent(4) | Indent each line by spaces |
Data Filters
| Filter | Description |
|---|---|
default('fallback') | Provide default for undefined/empty |
default('x', true) | Also use default for empty strings |
length | Return count of items |
first | Return first item |
last | Return last item |
join(',') | Join list items with separator |
sort | Sort items alphabetically |
sort(reverse=true) | Sort in descending order |
unique | Remove duplicate values |
reverse | Reverse the order |
map(attribute='name') | Extract attribute from each item |
selectattr('active') | Filter items where attribute is truthy |
rejectattr('disabled') | Filter items where attribute is falsy |
Output & Number Filters
| Filter | Description |
|---|---|
e or escape | HTML escape (security) |
safe | Mark as safe - skip auto-escaping |
tojson | Convert to JSON string |
pprint | Pretty-print for debugging |
round(2) | Round to decimal places |
abs | Absolute value |
int | Convert to integer |
float | Convert to float |
filesizeformat | Human-readable file size (1.2 MB) |
Filter Chaining
{{ names | sort | join(', ') | upper }}
Chain multiple filters left to right. Each filter receives output of previous.
Existence Tests
| Test | Usage |
|---|---|
defined | {% if var is defined %} - Variable exists |
undefined | {% if var is undefined %} - Variable doesn't exist |
none | {% if var is none %} - Value is None/null |
Type Tests
| Test | Description |
|---|---|
string | Is a string type |
number | Is a number (int or float) |
integer | Is an integer |
float | Is a float |
boolean | Is a boolean |
sequence | Is a list/sequence |
mapping | Is a dict/mapping |
iterable | Can be iterated over |
callable | Is a function/callable |
Value Tests
| Test | Usage |
|---|---|
odd | {% if num is odd %} |
even | {% if num is even %} |
divisibleby(3) | {% if num is divisibleby(3) %} |
eq(value) | {% if x is eq(y) %} - Equal to |
ne(value) | {% if x is ne(y) %} - Not equal to |
lt(value) | {% if x is lt(y) %} - Less than |
gt(value) | {% if x is gt(y) %} - Greater than |
in(seq) | {% if x is in(items) %} - In sequence |
Common Patterns
{% if var is defined and var %}
Check both exists AND has truthy value (not empty/zero).
{% if var is not none %}
Variable exists and is not null.
Template Inheritance
{% extends "base.html" %}
Inherit from a parent template.
{% block name %}...{% endblock %}
Define an overridable block.
{{ super() }}
Render parent block's content inside override.
Includes & Imports
{% include "file.html" %}
Include template with current context.
{% include "x" ignore missing %}
Don't error if file doesn't exist.
{% import "macros.html" as m %}
Import macros from another template.
{% from "x" import macro %}
Import specific macro by name.
Macros (Reusable Functions)
{% macro input(name, value='', type='text') %}
<input type="{{ type }}" name="{{ name }}" value="{{ value }}">
{% endmacro %}
{{ input('email', type='email') }}
Define reusable template functions with default arguments.
Whitespace Control
{%- ... -%}
Strip whitespace before/after tag.
{{- ... -}}
Strip whitespace around expression output.
{%- for item in items -%}
Use minus sign on either side to trim that direction. No space between brace and minus.
Raw Blocks & Escaping
{% raw %}...{% endraw %}
Output content without processing Jinja syntax.
{{ var | e }}
HTML escape - prevent XSS attacks.
{{ html_var | safe }}
Mark trusted HTML as safe (skip escaping).