Skip to content

Instantly share code, notes, and snippets.

@MatTheCat
Created November 14, 2016 10:49
Show Gist options
  • Save MatTheCat/f6ded656666611462a219adf0b85efa6 to your computer and use it in GitHub Desktop.
Save MatTheCat/f6ded656666611462a219adf0b85efa6 to your computer and use it in GitHub Desktop.
Implémentation basique de instanceof pour Twig
<?php
class InstanceOfTest 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(')');
}
}
/**
* Register:
* $twig->addTest(new \Twig_SimpleTest('instanceof', null, ['node_class' => 'InstanceOfTest']));
*
* Use:
* {% if object is instanceof('class') %}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment