How to check if a vari­able or val­ue exists using Twig

Expected results for conditional tests using is defined, length, is not empty, is not null

Below is a reference for the expected results for numerous conditional tests you can perform in your Twig templates. For a broader discussion and examples on how and when to use these conditional tests, see Testing if something exists: is defined, length, is not null, is not empty

devMode is defined variable length is not empty is not null
nonExistentVariable true False
Template Error: Variables does not exist
nonExistentVariable false False False False False False
nonExistentVariable.attribute true False
Template Error: Variable does not exist
nonExistentVariable.attribute false False False False False False
nonExistentVariable['attribute'] true False
Template Error: Impossible to access a key ("nonExistentAttribute") on a null variable
nonExistentVariable['attribute'] false False False False False False
string true/false True True True True True
string (empty) true/false True False False False True
0 true/false True False True True True
1 true/false True True True True True
true true/false True True True True True
false true/false True False False False True
null true/false True False False False False

Associative and multi-dimensional arrays (Hashes)

Twig refers to associative arrays and multi-dimensional arrays as hashes (an abbreviation for hash table).

devMode is defined variable length is not empty is not null
array true/false True True True True True
array (empty) true/false True False False False True
array.attribute true/false True True True True True
array.nonExistentAttribute true False
Template Error: Key does not exist as the array is empty
array.nonExistentAttribute false False False False False False
array['attribute'] true/false True True True True True
array['nonExistentAttribute'] true False
Template Error: Key "nonExistentAttribute" for array with keys "keyName1, keyName2" does not exist
array['nonExistentAttribute'] false False False False False False
attribute(array, 'attribute') true/false True True True True True
attribute(array, 'nonExistentAttribute') true False
Template Error: Key "nonExistentAttribute" for array with keys "keyName1, keyName2" does not exist
attribute(array, 'nonExistentAttribute') false False False False False False

Objects

The results below use the EntryModel as an example. In Craft 2, the only reliable way to test for an attribute of your object (such as a custom field), is to use array syntax: object['attribute'].

devMode is defined variable length is not empty is not null
object true/false True True True True True
object.attribute true/false True True True True True
object.nonExistentAttribute true True
CException: Craft\EntryModel and its behaviors do not have a method or closure named "nonExistentAttribute".
object.nonExistentAttribute false True
Internal Server Error: Craft\EntryModel and its behaviors do not have a method or closure named "nonExistentAttribute".
object['attribute'] true/false True True True True True
object['nonExistentAttribute'] true False
Template Error: Key "nonExistentAttribute" in object with ArrayAccess of class "Craft\EntryModel" does not exist
object['nonExistentAttribute'] false False False False False False
attribute(object, 'attribute') true/false True True True True True
attribute(object, 'nonExistentAttribute') true True
CException: Craft\EntryModel and its behaviors do not have a method or closure named "nonExistentAttribute".
attribute(object, 'nonExistentAttribute') false True
Internal Server Error: Craft\EntryModel and its behaviors do not have a method or closure named "nonExistentAttribute".

Notes

  • True: Test returns True.
  • False: Test returns False.
  • True: Test only returns True. False results are wrong or throw errors.
  • True: Test will incorrectly return True in some cases when results should be False.

Conditionals: All true results in the table above indicate that your variable exists or has a value. The default empty and null were updated to test for a value using not empty and not null so they can be easily compared to the results testing for a variable alone or using the length filter.

Objects: Objects and arrays in Twig can typically be accessed and tested in the same way, however, currently, the most reliable way to access your object attributes is using array syntax: object['attribute']. This is due to how PHP allows objects to be defined using magic methods and how Twig tests for the existence of variables and methods on those objects. In Craft 2, when an object uses the magic __call() method, Twig gets confused and always returns true.

Optimization: No testing has been done to determine which of the above tests may be the most performant. In some cases, using the length or is not empty tests may add an additional query to your page load. Testing directly for the variable or using is not null appears to not add any additional queries to your page load.

Level up in Craft CMS with practical examples, snippets, and patterns.
Craft The Planet emails are sent out several times a week.