Built-in Tests

divisibleby

divisibleby checks if a variable is divisible by a number:

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

none

none returns true if the variable is none:

{{ var is none }}

even

even returns true if the given number is even:

{{ var is even }}

odd

odd returns true if the given number is odd:

{{ var is odd }}

sameas

sameas checks if a variable points to the same memory address than another variable:

{% if foo.attribute is sameas(false) %}
    the foo attribute really is the ``false`` PHP value
{% endif %}

constant

constant checks if a variable has the exact same value as a constant. You can use either global constants or class constants:

{% if post.status is constant('Post::PUBLISHED') %}
    the status attribute is exactly the same as Post::PUBLISHED
{% endif %}

defined

defined checks if a variable is defined in the current context. This is very useful if you use the strict_variables option:

{# defined works with variable names #}
{% if foo is defined %}
    ...
{% endif %}

{# and attributes on variables names #}
{% if foo.bar is defined %}
    ...
{% endif %}

empty

empty checks if a variable is empty:

{# evaluates to true if the foo variable is null, false, or the empty string #}
{% if foo is empty %}
    ...
{% endif %}