Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lelyfoto/twig-instanceof

Adds InstanceOf Expression as Twig Extension
https://github.com/lelyfoto/twig-instanceof

bundle instanceof php symfony symfony-bundle twig

Last synced: about 17 hours ago
JSON representation

Adds InstanceOf Expression as Twig Extension

Awesome Lists containing this project

README

        

# Twig InstanceOf Expression

[![Package version](https://img.shields.io/packagist/v/lelyfoto/twig-instanceof.svg?style=flat-square)](https://packagist.org/packages/lelyfoto/twig-instanceof)
[![PHP version](https://img.shields.io/packagist/php-v/lelyfoto/twig-instanceof.svg?style=flat-square)](https://packagist.org/packages/lelyfoto/twig-instanceof)
[![License](https://img.shields.io/packagist/l/lelyfoto/twig-instanceof.svg?style=flat-square)](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:
```bash
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.

```twig
{% if testObject is instanceof('\\Example\\TestInterface') %}
Do something with {{ testObject.show() }}
{% endif %}
```

## Use without Symfony

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

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