Expression Tests

Beside filters, there are also so-called “tests” available. Tests can be used to test a variable against a common expression.

To test a variable or expression, you add is plus the name of the test after the variable. For example, to find out if a variable is defined, you can do name is defined, which will then return true or false depending on whether name is defined in the current template context.

Tests can accept arguments, too. If the test only takes one argument, you can leave out the parentheses. For example, the following two expressions do the same thing:

{% if loop.index is divisibleby 3 %}
{% if loop.index is divisibleby(3) %}

defined

Return true if the variable is defined.

{% if variable is defined %}
  value of variable: {{ variable }}
{% else %}
  variable is not defined
{% endif %}

returns: "variable is not defined"

{% set variable = 'test' %}
{% if variable is defined %}
  value of variable: {{ variable }}
{% else %}
  variable is not defined
{% endif %}

returns: "value of variable: test"

divisibleby

(num)

Check if a variable is divisible by a given number, num.

{% set number = 30 %}
{% set test = number is divisibleby(5) %}
{{test}}

outputs: true

{% if number is divisibleby(5) %}
  {{number }} is divisible by 5.
{% else %}
  Nope.
{% endif %}

outputs: "30 is divisible by 5."

equalto

Check if an object has the same value as another object.

{% set number = 30 %}
{% if number is equalto(20) %}
  {{ number }} is 20.
{% else %}
  {{ number }} is not 20.
{% endif %}

outputs: "30 is not 20."

even

Returns true if the variable's value is even.

{% set number = 30 %}
{% if number is even %}
  {{ number }} is even.
{% else %}
  {{ number }} is odd.
{% endif %}

outputs: "30 is even."

iterable

Returns true if the variable is an iterable (sequence, dictionary, etc.).

{% set list = [1,2,3,4,5] %}
{% if list is iterable %}
  {{ list | join(', ')}}
{% else %}
  {{ list }}
{% endif %}

outputs: 1, 2, 3, 4, 5

lower

Returns true if the given variable is a string that is all lowercase.

{% set name = 'bob smith' %}
{% if name is lower %}
  {{ name | title }}
{% else %}
  {{ name }}
{% endif %}

outputs: Bob Smith

mapping

Returns true if the given variable is a dictionary.

{% set name = {'first': 'Bob', 'last': 'Smith'} %}
{% if name is mapping %}
  Hi {{ name.first }}!
{% else %}
  Hi {{ name }}!
{% endif %}

outputs: "Hi Bob!"

newerthan

Return true if version A is newer than version B.

none

Return true if the given variable is null / none.

{% set name = 'Jim' %}
{% if name is none %}
  Hey there!
{% else %}
  Hi {{ name }}!
{% endif %}

outputs: Hi Jim!

👍

Use this test to check any outputted values. Leanplum will not send a message if any outputted value evaluates to null / none.

number

Return true if the object is a number.

{% set num = '3' %}
{% if num is number %}
  {{ num + 9 }}
{% else %}
  {{ num | int + 9 }}
{% endif %}

converts to int and outputs: 12

odd

Return true if the object is an odd number.

{% set num = 3 %}
{% if num is odd %}
  {{ num }} is odd.
{% else %}
  {{ num }} is even.
{% endif %}

ouputs: 3 is odd.

olderthan

Return true if version A is older than version B.

sameas

Returns true if variable is pointing at same object in memory as other variable.

{% set name = {'name': 'bob'} %}
{% set name2 = {'name': 'bob'} %}
{% if name is sameas name2 %}
  it is the same.
{% else %}
  it is different.
{% endif %}

outputs: it is different

{% set name = {'name': 'bob'} %}
{% set name2 = name %}
{% if name is sameas name2 %}
  it is the same.
{% else %}
  it is different.
{% endif %}

outputs: it is the same.

sequence

Returns true if the variable is a sequence. Sequences are variables that are iterable.

string

Return true if object is a string.

truthy

Return true if object is 'truthy'.

undefined

Return true if object is undefined.

upper

Return true if string is all uppercased.