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.
The simplest form of expressions are literals. Literals represent variables like strings and numbers. The following literals exist:
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).
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).
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:
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.
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.
Calculate the remainder of an integer division. This would return 4:
Multiply the left operand with the right one.
These operators return either
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
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.
For if statements (see Control Flow) and filtering, it can be useful to combine multiple expressions:
Return true if the left and the right operand are true.
Return true if the left or the right operand are true.
negate a statement.
group an expression.
The following operators are very useful but don’t fit into any of the other two categories:
Perform a sequence / mapping containment test. Returns true if the left operand is contained in the right. The following returns true:
Performs an expression test. The following passes the value
Applies a filter.
Converts all operands into strings and concatenates them.
Call a callable:
Inside of the parentheses you can use positional arguments and keyword arguments:
Get an attribute of an object.
Updated 9 months ago