Leanplum's user guides and developer documentation.

Leanplum Documentation

Leanplum's user guides, SDK setup, API docs, and more resources are here to help you get the most out of A/B testing, Campaigns, Messaging, and Analytics.

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 variable

description

index

The current iteration of the loop. (1 indexed)

index0

The current iteration of the loop. (0 indexed)

revindex

The number of iterations from the end of the loop (1 indexed)

revindex0

The number of iterations from the end of the loop (0 indexed)

first

True if first iteration.

last

True if last iteration.

length

The number of items in the sequence.

cycle

A helper function to cycle between a list of sequences. See the explanation below.

depth

Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1

depth0

Indicates 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.

Updated 11 months ago


Control Structures


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.