Control Structures

A control structure refers to all those things that control the flow of a program - conditionals (i.e. if/elif/else), for-loops, as well as things like macros and blocks. With the default syntax, control structures appear inside {% ... %}blocks.

🚧

All HTML should be written in the source code editor <>

Examples that include HTML will only work if written in the source code window. Raw HTML cannot be pasted into the regular message body section.

For loops

Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:

<h1>Members</h1>
<ul>
{% for user in users %}
  <li>{{ user.username|e }}</li>
{% endfor %}
</ul>

As variables in templates retain their object properties, it is possible to iterate over containers like dictionaries:

<dl>
{% for key, value in my_dict %}
  <dt>{{ key | e }}</dt>
  <dd>{{ value | e }}</dd>
{% endfor %}
</dl>

Inside of a for-loop block, you can access some special variables:

for loop variabledescription
indexThe current iteration of the loop. (1 indexed)
index0The current iteration of the loop. (0 indexed)
revindexThe number of iterations from the end of the loop (1 indexed)
revindex0The number of iterations from the end of the loop (0 indexed)
firstTrue if first iteration.
lastTrue if last iteration.
lengthThe number of items in the sequence.
cycleA helper function to cycle between a list of sequences. See the explanation below.
depthIndicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1
depth0Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 0
{%for item in itemsList%}
	{%if loop.last == true%}
		This is the last item in the list: {{item}}
	{%endif%}
{%endfor%}

Within a for-loop, it’s possible to cycle among a list of strings/variables each time through the loop by using the special cycle helper:

{% for row in rows %}
  <li class="{% cycle('odd', 'even') %}">{{ row }}</li>
{% endfor %}

You can call variables that are defined outside of a loop from within a loop, but not the other way around.

It’s not possible to break or continue in a loop. You can, however, filter the sequence during iteration, which allows you to skip items. The following example skips all the users which are hidden:

{% for user in users if not user.hidden %}
  <li>{{ user.username|e }}</li>
{% endfor %}

The advantage is that the special loop variable will count correctly; thus not counting the users not iterated over.

If no iteration took place because the sequence was empty or the filtering removed all the items from the sequence, you can render a default block by using else:

<ul>
{% for user in users %}
  <li>{{ user.username|e }}</li>
{% else %}
  <li><em>no users found</em></li>
{% endfor %}
</ul>

If statements

In the simplest form, you can use it to test if a variable is defined, not empty or not false:

{% if userAttribute.interests %}
Your interests:
<ul>
{% for interest in userAttribute.interests %}
  <li>{{ interest|e }}</li>
{% endfor %}
</ul>
{% endif %}

For multiple branches, elif and else can be used too:

{% if userAttribute.membership == 'Gold' %}
  You are a Gold member!
{% elif userAttribute.membership == 'Silver' %}
  You are a Silver member!
{% else %}
  Please become a member today!
{% endif %}

It is also possible to use inline if expressions:

{{ "You're a gold star member!" if userAttribute.membership == 'Gold' else  "Please become a member today!"}}

The general syntax is if else . The else clause is optional.