fabpot/Twig — Issue #553: Consider an instanceof test
Author: jonathaningram
Created: 2011-12-09 02:25:06+00:00
State: open

I am not sure if this is against twig's beliefs, but here is what I've got. If you think it's useful, I'll create a PR.

```php

* {% if object is aninstanceof('Some\\Class') %}
*

*/
class Twig_Node_Expression_Test_Aninstanceof extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' instanceof ')
->raw($this->getNode('arguments')->getNode(0)->getAttribute('value'))
->raw(')')
;
}
}
```

Note: it cannot be named `Instanceof` since it's obviously a reserved word.

Register it in an extension:

```php
new Twig_Test_Node('Twig_Node_Expression_Test_Aninstanceof'),
);
}
```

Then use it like this:

```jinja
{%- if variable is aninstanceof("Some\\Class") -%}

{%- endif -%}
```