Tests

The following tests are available to Twig templates in Craft:

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

# 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, an interface used to represent components whose types are missing.

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