Control Statement
If statements
Determines which branch to output by determining the truth or falsity of an expression. Syntax keywords: if
, elif
, else
, endif
, if
and endif
are mandatory and occur in pairs.
{% if price < 10 or always_show %}
Price is {{ price }}.
{% elif price > 1000 and not rich %}
That's expensive!
{% else %}
N/A
{% else %}
Undefined variables are considered false, so they are used in if statements to test whether a variable is defined without raising an error.
{% if my_var %}
{{ my_var }}
{% else %}
Sorry, my_var isn't defined.
{% endif %}
For statements
Loops through each item in the list, with the syntax keywords for
and endfor
, which must occur in pairs.
{% for product in products %}
{{loop.index}}. {{product.name}}
{% endfor %}
Or each character in the string
{% for letter in name %}
{% if loop.index % 2 == 0%}
<span style="color:red">{{ letter }}</span>
{% else %}
<span style="color:blue">{{ letter }}</span>
{% endif %}
{% endfor %}
The object variable loop
is predefined inside the loop and has the following properties available:
loop.index
: the order of the current loop, starting from 1.loop.index0
: the order of the current loop, from 0.loop.first
: whether the loop is the first one or notloop.last
: if or not this is the last loop.
Include statements
The include statement can include other templates, syntax: include <path>
, the path is always found from the template root directory, can not be preceded by /
, relative paths are not supported.
{% include "included.html" %}
The path can be a string expression
{% include "partials/" ~ name ~ ".html" %}
The template to be included:
- Shared current context data
- Newly assigned variables, valid only within them
Errors are reported when a template is not found, you can use the optional
keyword to ignore templates when they do not exist.
{% include "header.html" optional %}
The include statement supports path lists, where the first template that exists is included and the others are ignored, as well as optional
.
{% include ["custom/header.html", "header.html"] %}
{% include ["special_sidebar.html", "sidebar.html"] optional %}
Macros statement
Macros can be thought of as a special form of function whose internal context is isolated from the outside world by parameter passing, and whose rendered output is returned as a whole.
{% macro input(label, type="text") %}
<label>
{{ label }}
<input type="{{type}}" />
</label
{% endmacro input %}
Its parameters support constant defaults. Parameter values can be valid variables, filters and functions.
Macros need to be defined in a separate template file, imported and namespaces specified when used:
{% import "macros.html" as macros %}
The form of the call is as follows:
{{ macros::input(label="Name", type="text") }}
In the macro definition file, use the self
namespace when you need to use other macro modules internally, with self
it is also possible to make recursive calls, be careful to determine when you have finished .
{% macro factorial(n) %}
{% if n > 1 %}{{ n }} - {{ self::factorial(n=n-1) }}{% else %}1{% endif %}
{% endmacro factorial %}
Macro modules support all syntax except block
, extends
.