Jinja Template Builder

Build configuration templates with Jinja2 syntax highlighting

Select a pre-built template to load into the editor
Statements
Expressions
Comments
Filters
Template Editor 0 characters

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

VariableDescription
loop.indexCurrent iteration (1-indexed)
loop.index0Current iteration (0-indexed)
loop.firstTrue if first iteration
loop.lastTrue if last iteration
loop.lengthTotal number of items
loop.cycle('a','b')Cycle through values each iteration
loop.previtemPrevious item (undefined on first)
loop.nextitemNext 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

FilterDescription
upperConvert to uppercase
lowerConvert to lowercase
capitalizeCapitalize first character
titleTitle Case Each Word
trimRemove 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

FilterDescription
default('fallback')Provide default for undefined/empty
default('x', true)Also use default for empty strings
lengthReturn count of items
firstReturn first item
lastReturn last item
join(',')Join list items with separator
sortSort items alphabetically
sort(reverse=true)Sort in descending order
uniqueRemove duplicate values
reverseReverse 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

FilterDescription
e or escapeHTML escape (security)
safeMark as safe - skip auto-escaping
tojsonConvert to JSON string
pprintPretty-print for debugging
round(2)Round to decimal places
absAbsolute value
intConvert to integer
floatConvert to float
filesizeformatHuman-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

TestUsage
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

TestDescription
stringIs a string type
numberIs a number (int or float)
integerIs an integer
floatIs a float
booleanIs a boolean
sequenceIs a list/sequence
mappingIs a dict/mapping
iterableCan be iterated over
callableIs a function/callable

Value Tests

TestUsage
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).