lelyfoto/twig-instanceof

Adds InstanceOf Expression as Twig Extension

Installs: 14 074

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 1

Forks: 2

Open Issues: 1

Type:symfony-bundle

v1.2 2024-01-02 09:05 UTC

This package is auto-updated.

Last update: 2024-05-01 00:11:45 UTC


README

Package version PHP version License

Adds InstanceOf Expression as Twig Extension. When you want to check for a specific type of variable in a twig template.

Prerequisites

  • PHP 8.0+
  • Symfony 5.4 or 6.x
  • Twig 3.x

Installation

  1. Install this Symfony bundle with composer:
    composer require lelyfoto/twig-instanceof
  2. Make sure the bundle is added to config/bundles.php:
    return [
        // ...
        Lelyfoto\Twig\InstanceOf\TwigInstanceOfBundle::class => ['all' => true],
        // ...
    ];   
    

Usage

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 without Symfony

use Twig\Environment;
use Lelyfoto\Twig\InstanceOf\InstanceOfExtension;

$twig = new Environment($loader);
$twig->addExtension(new InstanceOfExtension());