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:
Strings | Everything 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).
|
---|---|
Numbers | Integers 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).
|
Lists | Everything between two brackets is a list.
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:
|
Dictionaries | A 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.
|
Booleans |
|
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.
| |
---|---|
Subtract the second number from the first one.
| |
/ | Divide two numbers. The return value will be a floating point number.
With variables:
|
% | Calculate the remainder of an integer division. This would return 4:
|
Multiply the left operand with the right one.
|
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.
|
---|---|
| Compares two objects for inequality.
|
| true if the left hand side is greater than the right hand side.
|
| true if the left hand side is greater or equal to the right hand side.
|
| true if the left hand side is lower than the right hand side.
|
| true if the left hand side is lower or equal to the right hand side.
|
Logical operators
For if statements (see Control Flow) and filtering, it can be useful to combine multiple expressions:
and | Return true if the left and the right operand are true.
|
---|---|
or | Return true if the left or the right operand are true.
|
not | negate a statement.
|
(expr) | group an expression.
|
Other operators
The following operators are very useful but don’t fit into any of the other two categories:
in | Perform a sequence / mapping containment test. Returns true if the left operand is contained in the right. The following returns true:
|
---|---|
is | Performs an expression test. The following passes the value
|
| | Applies a filter. |
~ | Converts all operands into strings and concatenates them.
Returns |
() | Call a callable:
Inside of the parentheses you can use positional arguments and keyword arguments:
|
| Get an attribute of an object.
or
See Variables. |
Updated 5 months ago