Cheatography
https://cheatography.com
Jinja document templating
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Jinja Tokens
|
Control statements |
|
Replacement expressions |
|
Comments |
Whitespace control
|
Disable strip behavior to include extra whitespace |
|
Remove whitespace around blocks |
These modifiers can be added to the beginning or the end of a Jinja control token. {%-
or -%}
. In the case of +
, the position controls where the trim is disabled. They can be applied to any control token {%- %}
, {{- }}
, or {#- #}
.
Control blocks
{% if <statement> %}
... {% elif <statement> %}
... {% else %}
... {% endif %}
|
{% for <item> in <list> [recursive] %} ... {% endfor %}
|
{% block <name> [scoped] [required] %}
... {% endblock %}
|
|
{% macro <name>(<args...>) %}
... {% endmacro %}
|
{% set <name> %}
... {% endset %}
|
|
|
Filters
Variables can be modified with filters. To apply a filter use a pipe, ' |
'. For example, {{ name|upper }}
.
Filters can be chained together. For example, {{ name|striptags|title }}
will strip all HTML tags from name
then convert it to Title case.
Some filters allow arguments. See the Jinja documentation for details. |
Assignments and macros
Variable assignment Simple variables can be assigned by using the set
block: {% set variable='value' %}
|
Block assignments Templated blocks can be assigned to variable names by using a set
block. {% set reply %} Thanks for your message: {{ message }}. {% endset %}
|
|
|
Loop special variables
The current iteration of the loop. (1 indexed)
|
The current iteration of the loop. (0 indexed)
|
The number of iterations from the end of the loop (1 indexed)
|
The number of iterations from the end of the loop (0 indexed)
|
True if first iteration.
|
True if last iteration.
|
The number of items in the sequence.
|
A helper function to cycle between a list of sequences.
|
Indicates how deep in a recursive loop the rendering currently is. Starts at level 1
|
Indicates how deep in a recursive loop the rendering currently is. Starts at level 0
|
The item from the previous iteration of the loop. Undefined during the first iteration.
|
The item from the following iteration of the loop. Undefined during the last iteration.
|
True if previously called with a different value (or not called at all).
|
|