fabpot/Twig — Issue #595: new tests to check variable type
Author: inmarelibero
Created: 2012-01-14 12:13:29+00:00
State: open

I didn't find any discussion about this, neither issues.

What about adding to Twig core ests about the type of variable?

It could be achieved in various way.

**1) many tests like:**

{% if var is string %}

Looking at [PHP gettype() function](http://php.net/manual/en/function.gettype.php), I would suggest:

{% if var is boolean %}
{% if var is integer %}
{% if var is double %}
{% if var is string %}
{% if var is array %}
{% if var is object %}
{% if var is resource %}

**2) a new filter:**

{% var | type %}

`type` filter would return a value specified in [PHP gettype() function](http://php.net/manual/en/function.gettype.php), letting it possible to use:

{% if var | type is sameas('string') %}
{% if var | type is sameas('array') %}
...

**3) a new comparison operator:**

{% if var is type of 'string' %}
and

{% if var is not type of 'string' %}

I don't know which is preferable:
2) lets know the type of a var independently by being in a `if` statement
3) is more elegant but ca be used only in a `if`

what do you think? maybe can be implemented both 2) and 3), they would be redundant only partially