Craft-TwigTypeTest
Test if a Twig Variable is of a certain type in Craft-CMS.
##Usage:
####Test: of_type('test_name')
Check if a variable is of a specific type.
{% set test_var = craft.entries %}
{% if test_var is of_type('object') %}
true
{% endif %}
Check if a variable is an array of a specific class.
TODO: This currently only checks for a certain class, extend this to check for arrays of other types as well.
{% set test_var = craft.entries.section("Things").find() %}
{% if test_var is of_type('array_of', 'Craft\\EntryModel) %}
true
{% endif %}
Available Tests:
- array
- bool
- class
- float
- int
- numeric
- object
- scalar
- string
- array_of true if the var is an array and the first entry (as returned by array_shift) is of the specified class
get_type
Filter: Return the type of a variable.
{% set test_var = craft.entries %}
{{ test_var|get_type }}
will output:
object
Alias for php's gettype()
get_class
Filter: Return the class of a variable.
{% set test_var = craft.entries.section("Things").find() %}
{{ test_var[0]|get_type }}
will output:
Craft\EntryModel
Alias for php's get_class()
##Install:
- Move the
twigtypetest
directory into thecraft/plugins/
directory. - Go to Settings -> Plugins and enable 'Twig Type Tests'.