Tests are used to perform logical operations within templated expressions such as checking if a variable is defined or if a variable is empty.

Each section below represents a built-in test.

defined

The defined test checks if a variable is defined.

twig
{% if missing is not defined %}
    ...
{% endif %}

empty

The empty test checks if a variable is empty. A variable is empty if it is null, an empty string, an empty collection, or an empty map.

twig
{% if user.email is empty %}
    ...
{% endif %}

even

The even test checks if an integer is even.

twig
{% if 2 is even %}
    ...
{% endif %}

iterable

The iterable test checks if a variable implements java.lang.Iterable.

twig
{% if users is iterable %}
    {% for user in users %}
        ...
    {% endfor %}
{% endif %}

json

The json test checks if a variable is valid json string

twig
{% if '{"test": 1}' is json %}
    ...
{% endif %}

map

The map test checks if a variable is an instance of a map.

twig
{% if {"apple":"red", "banana":"yellow"} is map %}
    ...
{% endif %}

null

The null test checks if a variable is null.

twig
{% if user.email is null %}
    ...
{% endif %}

odd

The odd test checks if an integer is odd.

twig
{% if 3 is odd %}
    ...
{% endif %}

Was this page helpful?