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: 9 months ago
JSON representation
Adds InstanceOf Expression as Twig Extension
- Host: GitHub
- URL: https://github.com/lelyfoto/twig-instanceof
- Owner: lelyfoto
- License: mit
- Created: 2022-02-11T00:37:05.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-21T14:30:06.000Z (over 1 year ago)
- Last Synced: 2025-04-23T08:54:12.514Z (9 months ago)
- Topics: bundle, instanceof, php, symfony, symfony-bundle, twig
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Twig InstanceOf Expression
[](https://packagist.org/packages/lelyfoto/twig-instanceof)
[](https://packagist.org/packages/lelyfoto/twig-instanceof)
[](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());
```