Tests

The following tests 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 | Whether a variable is the same as a PHP constant value. countable | Whether a variable is a countable 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. 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 | 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. numeric | Whether a variable is numeric. object | Whether a variable is an object. odd | Whether a number is odd. resource | Whether a variable is a resource. same as | 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() 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() method.

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

callable

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

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

countable

Returns whether an object is a countable value via PHP’s is_countable() 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.

{{ 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() 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() 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 craft3:craft\base\MissingComponentInterface, 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() 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() 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() 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() 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() method.

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

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