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 %}

Learn more about the empty test

even

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

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

Learn more about the even test

float

Checks to see if the value is a float.

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

Learn more about the float test

integer

Checks to see if the value is an integer.

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

Learn more about the integer test

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 %}

Learn more about the iterable test

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 %}

Learn more about the null test

numeric

Checks to see if the value is a number.

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

Learn more about the numeric test

odd

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

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

Learn more about the odd test

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 %}

Learn more about the same as test

string

Checks to see if the value is a string

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

Learn more about the string test