Statements
Loop over each item in a sequence.
Inside of a for-loop block, you can access some special variables:
Macro are useful to put often used idioms into reusable component.
Note that macros are hoisted, and their scope is the toplevel scope. It means that you define two macro with the same name, the second will erase the first, even in the macro invocation occuring before the second definition.
Use call
statement to pass a macro to another macro.
Function can be seen as macros that return a value instead of printing it.
The if statement allows us to check if an expression is true or false, and execute different code according to the result.
0
, null
, ''
, ""
, []
,
or {}
are considered as false
when used in a if
/elif
test.
The rest is true
.
All blocks introduce a new scope when assigning variables. You can read a variable defined in a parent block, but you can not assign a new value to this variable. A new variable will be created for each assignment. It will shadow the old definition, but as soon as you will leave the block, the old definition
The only exception to that rule are if
statements which do not introduce a scope.
See Namespace for variable assignement propagating across scopes.
Namespace are used to create variables you can modify from a child block.
Use {%-
to strip whitespaces before this token.
Use -%}
to strip whitespaces after this token.
Tests
Tests are just functions that return a boolean.
Tests can be used with the standard function call syntax,
but also with the is
keyword in statements or expressions.
Operators
Evaluation of boolean operators is sequential, left-to-right.
In e1 && e2
,
e1
is evaluated first, and if it returns false
,
e2
is not evaluated at all.
In e1 || e2
,
e1
is evaluated first, and if it returns true
, e2
is not evaluated at all.
Use not
and !
operators to negate a boolean.
Built-in filters
abs (num)
Return the absolute value of num
.
attr (name, obj)
{{ foo | attr ("bar") }}
is {{ foo.bar }}
.
{{ foo | attr ("bar.baz") }}
is {{ foo.bar.baz }}
.
default (default_value, value)
Return value
if it is defined from null
,
and default
otherwise.
escape (string)
. Replaces &
, "
,
<
and >
with their corresponding HTML entities.
groupby (fn, seq)
join (sep, seq)
last (seq)
.
Return the last element of sequence seq
.
length (seq)
.
Return the number of elements in sequence seq
.
range (start, stop)
round (method, val)
. method
can be "floor"
or "ceil"
.
sort (seq)
. Support the following optionnal keywords:
strlen (string)
. Number of UTF-8 characters.