/twigtypetest

Twig Variable Type Tests for Craft-CMS

Primary LanguagePHPMIT LicenseMIT

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:

Filter: get_type

Return the type of a variable.

{% set test_var = craft.entries %}

{{ test_var|get_type }}

will output: object

Alias for php's gettype()

Filter: get_class

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:

  1. Move the twigtypetest directory into the craft/plugins/ directory.
  2. Go to Settings -> Plugins and enable 'Twig Type Tests'.