Tests

You are viewing documentation for an unreleased version of Craft CMS. Please be aware that some pages, screenshots, and technical reference may still reflect older versions.

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

Test Description
array Whether a variable is an array.
boolean Whether a variable is a boolean value.
callable Whether a variable is callable.
constant (opens new window) Whether a variable is the same as a PHP constant value.
countable Whether a variable is a countable 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.
float Whether a variable is a float value.
instance of Whether an object is an instance of a namespace or parent class.
integer Whether a variable is an integer value.
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.
numeric Whether a variable is numeric.
object Whether a variable is an object.
odd (opens new window) Whether a number is odd.
resource Whether a variable is a resource.
same as (opens new window) Whether a variable is the same as another.
scalar Whether a variable is a scalar.
string Whether a variable is a string value.

# array

Returns whether an object is an array via PHP’s is_array() (opens new window) method.

{{ ['oh', 'hello', 'there'] is array ? 'true' : 'false' }}
{# result: true #}

{{ 'oh hai' is array ? 'true' : 'false' }}
{# result: false #}

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

# callable

Returns whether an object is callable via PHP’s is_callable() (opens new window) method.

{{ [entry, 'getStatus'] is callable ? 'true' : 'false' }}
{# result: true #}

# countable

Returns whether an object is a countable value via PHP’s is_countable() (opens new window) method.

is_countable() was added in PHP 7.3.0, so for versions less than 7.3 this returns true if the object is an array or instance of Countable (opens new window).

{{ craft.entries() is countable ? 'true' : 'false' }}
{# result: true #}

{{ ['apple', 'orange'] is countable ? 'true' : 'false' }}
{# result: true #}

{{ 'dracula' is countable ? 'true' : 'false' }}
{# result: false #}

# float

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

{{ 10.5 is float ? 'true' : 'false' }}
{# result: true #}

{{ 9 is float ? 'true' : 'false' }}
{# result: false #}

{{ 'duck' is float ? 'true' : 'false' }}
{# result: false #}

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

# integer

Returns whether an object is an integer via PHP’s is_int() (opens new window) method.

{{ 23 is integer ? 'true' : 'false' }}
{# result: true #}

{{ '23' is integer ? 'true' : 'false' }}
{# result: false #}

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

# numeric

Returns whether an object is numeric via PHP’s is_numeric() (opens new window) method.

{{ 23 is numeric ? 'true' : 'false' }}
{# result: true #}

{{ '23' is numeric ? 'true' : 'false' }}
{# result: true #}

{{ 'twenty-three' is numeric ? 'true' : 'false' }}
{# result: false #}

# object

Returns whether a given object satisfies PHP’s is_object() (opens new window) method.

{{ entry is object ? 'true' : 'false' }}
{# result: true #}

{{ entry.url is object ? 'true' : 'false' }}
{# result: false #}

# resource

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

{{ asset.getStream() is resource ? 'true' : 'false' }}
{# result: true #}

{{ siteUrl is resource ? 'true' : 'false' }}
{# result: false #}

# scalar

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

{{ 'hai' is scalar ? 'true' : 'false' }}
{# result: true #}

{{ 23 is scalar ? 'true' : 'false' }}
{# result: true #}

{{ [23, 'hai'] is scalar ? 'true' : 'false' }}
{# result: false #}

# string

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

{{ '23' is string ? 'true' : 'false' }}
{# result: true #}

{{ 23 is string ? 'true' : 'false' }}
{# result: false #}