Tests

Tests are used with the is operator to test a variable against an expression. The left operand is the thing being tested and the right operand is the name of the test (left and right of "is").

For example, to find out if the value of a variable is odd you could do:

{% if variable is odd %}
{% endif %}

variable is the value being tested and odd is the name of the test.

Negative tests

To do a negative test you would use the is not operator.

{% if variable is not defined %}
{% endif %}

List of available tests

defined

Checks to see if a variable is defined in the current context.

{% if foo is defined %}
   ...
{% endif %}

Learn more about the defined test

divisible by

Checks to see if the variable value is evenly divisible by a number.

{% if variable is divisible by(3) %}
...
{% endif %}

Learn more about the divisible by test

empty

Tests to see if the value is an empty string, an empty array, an empty hash, exactly false, or exactly null.

{% if var is empty %}
...
{% endif %}

even

Tests to see if the value of the variable is a number and is even.

{% if var is even %}
...
{% endif %}

float

Checks to see if the value is a float.

{% if var is float %}
{% endif %}

integer

Checks to see if the value is an integer.

{% if var is integer %}
{% endif %}

iterable

Tests a variable to see if it is an array or a traversable object. Note that it doesn't test if the array or object has any values.

{% if posts is iterable %}
<p>posts is an array and may have some values to loop through.</p>
{% endif %}

null

Tests to see if the value of the variable is exactly null.

{% if variable is null %}
<p>the value of the variable is NULL</p>
{% endif %}

numeric

Checks to see if the value is a number.

{% if var is numeric %}
{% endif %}

odd

Tests to see if the value of the variable is a number and is odd.

{% if number is odd %}
...
{% endif %}

same as

Tests to see if the value of the variable is exactly the same as another variable or value. It's the equivalent of an "===" test.

{% if variable is same as(false) %}
<p>the value of the variable is false</p>
{% endif %}

string

Checks to see if the value is a string

{% if var is string %}
{% endif %}