https://github.com/intaro/twig-injection-bundle
Allow to inject the twig templates through the event behavior
https://github.com/intaro/twig-injection-bundle
Last synced: 11 months ago
JSON representation
Allow to inject the twig templates through the event behavior
- Host: GitHub
- URL: https://github.com/intaro/twig-injection-bundle
- Owner: intaro
- License: mit
- Created: 2014-08-04T13:20:18.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2024-10-11T10:38:51.000Z (almost 2 years ago)
- Last Synced: 2025-06-26T00:04:44.748Z (about 1 year ago)
- Language: PHP
- Size: 17.6 KB
- Stars: 7
- Watchers: 5
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TwigInjectionBundle
The TwigInjectionBundle allows to inject twig templates through the event behavior.
## Installation
TwigInjectionBundle requires Symfony 3.4 or higher.
Require the bundle in your `composer.json` file:
```json
{
"require": {
"intaro/twig-injection-bundle": "^2.0"
}
}
```
Register the bundle in `AppKernel`:
```php
// app/AppKernel.php
public function registerBundles()
{
$bundles = [
//...
new Intaro\TwigInjectionBundle\IntaroTwigInjectionBundle(),
];
//...
}
```
Install the bundle:
```
$ composer update intaro/twig-injection-bundle
```
## Usage
1) Add `{{ inject() }}` calling in template:
```twig
{{ inject('twig.injection.event.name', { parameter1: 'some-value', parameter2: some_object }) }}
```
2) Prepare controller action which you want to render or template which you want to include.
3) Define Listener which will inject `include` or `render` calling:
```php
getParameters();
if (!isset($parameters['parameter1']) || 'some-value' !== $parameters['parameters1']) {
return;
}
$render = new TwigInjectRender(
'AcmeDemoBundle:DefaultController:index',
[ 'object' => $parameters['parameters2'] ]
);
$event->addInjection($render);
$include = new TwigInjectInclude('AcmeDemoBundle:Default:someTemplate.html.twig');
$event->addInjection($include);
}
}
```
4) Register the listener:
```yaml
services:
acme_demo.twig_injection.listener:
class: Acme\DemoBundle\EventListener\TwigInjectionListener
tags:
- { name: kernel.event_listener, event: twig.injection.event.name, method: onSomeEvent }
```