Expressions

Leanplum allows basic expressions everywhere. Expressions can be used to create variables from other variables or expressions, such as by performing mathematical, logical, or other operations.

Literals

The simplest form of expressions are literals. Literals represent variables like strings and numbers. The following literals exist:

StringsEverything between two double or single quotes is a string. They are useful whenever you need a string in the template (e.g. as arguments to function calls and filters, or just to extend or include a template).

“Hello World”
NumbersIntegers and floating point numbers are created by just writing the number down. If a dot is present, the number is a float, otherwise an integer. Keep in mind that 42 and 42.0 are different (int and float, respectively).

42 / 42.23
ListsEverything between two brackets is a list.
['list', 'of', 'objects']
Lists are useful for storing sequential data to be iterated over. For example, you can easily create a list of links using lists and tuples for (and with) a for loop:

<ul> {% for href, caption in [ ('index.html', 'Index'), ('about.html', 'About'), ('downloads.html', 'Downloads') ] %} <li><a href="{{ href }}"> {{ caption }} </a></li> {% endfor %} </ul>
DictionariesA dictionary combines keys and values. Keys must be unique and always have exactly one value. Dictionaries are rarely created, but they’re used when reading data from other sources.
{ 'dictionary': 'of', 'key': ‘and', 'value': 'pairs' }
Booleanstrue is always true and false is always false.
true / false

Mathematical operators

Leanplum allows you to calculate with values. The following operators are supported:

+Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator.
{{ 1 + 1 }}
-Subtract the second number from the first one.
{{ 3 - 2 }}
/Divide two numbers. The return value will be a floating point number.
{{ 1 / 2 }}
With variables:
{% set totalLevels = 15 %} Your average score per level is: {{ userAttribute['totalPoints'] / totalLevels }}.
%Calculate the remainder of an integer division. This would return 4:
{{ 11 % 7 }}
*Multiply the left operand with the right one.
{{ 2 * 2 }}

Comparison operators

These operators return either true or false depending on the two values being compared. These are most commonly used with if Control Flow statements to vary the content of a message.

🚧

Note for email comparisons

Greater than (>) and less than (<) operators don't work in email messages or email previews built with our Legacy Messaging. You can however, use them in Campaign Composer.

==Compares two objects for equality.
{% if 3 == 3 %} They're the same. {% endif %}
!=Compares two objects for inequality.
{% if 3 != 4 %} They're different. {% endif %}
>true if the left hand side is greater than the right hand side.
{% if userAttribute['points'] > 999 %} You've reached Elite status! {% endif %}
>=true if the left hand side is greater or equal to the right hand side.
{% if userAttribute['points'] >= 500 %} You've reached Gold status! {% endif %}
<true if the left hand side is lower than the right hand side.
{% if occurrencesOf['levelUp'] < 2 %} You can do it. Push yourself to Level up. {% endif %}
<=true if the left hand side is lower or equal to the right hand side.
{% if lifetimeValue < 200 %} Check out our 50% off sale! {% endif %}

Logical operators

For if statements (see Control Flow) and filtering, it can be useful to combine multiple expressions:

andReturn true if the left and the right operand are true.
{% set goldStatus = userAttribute['goldStatus'] %} {% set active = userAttribute['activeMember'] %} {% if goldStatus and active %} You're a gold star member! {% endif %}
orReturn true if the left or the right operand are true.
{% if userAttribute.age > 30 or userAttribute.status == "gold" %} Either is true. {% endif %}
or can also be used to deal with default values. For example, the following will return the user’s name if it exists; otherwise, it will return the user’s email:
{{ userAttribute.name or userAttribute.email }}
notnegate a statement.
{% if not (goldStatus and active) %} Apply for Gold status now! {% endif %}

not can also be used inline with in and is expressions:

{% if 3 is not even %} It's odd! {% endif %} {% if 3 not in [1, 2, 4] %} Go fish! {% endif %}
(expr)group an expression.
{% if (goldStatus and active) and 3 is odd %} Everything's true! {% endif %}

Other operators

The following operators are very useful but don’t fit into any of the other two categories:

inPerform a sequence / mapping containment test. Returns true if the left operand is contained in the right. The following returns true:
{{ 1 in [1, 2, 3] }}
isPerforms an expression test. The following passes the value 3 to the odd expression test function, and returns true:
{{ 3 is odd }}
|Applies a filter.
~Converts all operands into strings and concatenates them.
{% set name = "John" %} {{ "Hello " ~ name ~ "!" }}
Returns HelloJohn!.
()Call a callable:
{{ post.render() }}
Inside of the parentheses you can use positional arguments and keyword arguments:
{{ post.render(user, full=true) }}
. or []Get an attribute of an object.
{{ userAttribute.firstName }}
or
{{ userAttribute['firstName'] }}
See Variables.