Tests

The following tests (opens new window) are available to Twig templates in Craft:

Test Description
boolean Whether a variable is the same as a PHP constant value.
constant (opens new window) Whether a variable is the same as a PHP constant value.
defined (opens new window) Whether a variable is defined.
divisible by (opens new window) Whether a number is divisible by another number.
empty (opens new window) Whether a variable is empty.
even (opens new window) Whether a number is even.
instance of Whether an object is an instance of a namespace or parent class.
iterable (opens new window) Whether a variable is an array or a traversable object.
missing Whether an object is missing its expected class.
null (opens new window) Whether a variable is null.
odd (opens new window) Whether a number is odd.
same as (opens new window) Whether a variable is the same as another.

# boolean

Returns whether an object is a boolean via PHP’s is_bool() (opens new window) method.

{% if myVar is boolean %}
    {{ myVar ? 'true' : 'false' }}
{% endif %}

# instance of

Returns whether an object is an instance of another object or class.

{% if element is instance of('craft\\elements\\Entry') %}
    <h1>{{ entry.title }}</h1>
{% endif %}

# missing

Returns whether a given object is an instance of craft\base\MissingComponentInterface (opens new window), an interface used to represent components whose types are missing.

{% if field is missing %}
    <p>😱</p>
{% endif %}