Adds InstanceOf Expression as Twig Extension. When you want to check for a specific type of variable in a twig template.
composer require lelyfoto/twig-instanceof
config/bundles.php
:
return [
// ...
Lelyfoto\Twig\InstanceOf\TwigInstanceOfBundle::class => ['all' => true],
// ...
];
Important to note that when testing for a class that you must use quotes and escaping inside the twig templates.
{% if testObject is instanceof('\\Example\\TestInterface') %}
Do something with {{ testObject.show() }}
{% endif %}
use Twig\Environment;
use Lelyfoto\Twig\InstanceOf\InstanceOfExtension;
$twig = new Environment($loader);
$twig->addExtension(new InstanceOfExtension());