intaro / twig-injection-bundle
The TwigInjectionBundle allows to inject twig templates through the event behavior
Package info
github.com/intaro/twig-injection-bundle
Type:symfony-bundle
pkg:composer/intaro/twig-injection-bundle
v3.0.0
2024-10-11 10:37 UTC
Requires
- php: ^8.0
- symfony/event-dispatcher: ^4.4|^5.0|^6.0
- symfony/framework-bundle: ^4.4|^5.0|^6.0
- symfony/http-kernel: ^4.4|^5.0|^6.0
- symfony/twig-bridge: ^4.4|^5.0|^6.0
- symfony/twig-bundle: ^4.4|^5.0|^6.0
- symfony/yaml: ^4.4|^5.4|^6.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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:
{
"require": {
"intaro/twig-injection-bundle": "^2.0"
}
}
Register the bundle in AppKernel:
// app/AppKernel.php public function registerBundles() { $bundles = [ //... new Intaro\TwigInjectionBundle\IntaroTwigInjectionBundle(), ]; //... }
Install the bundle:
$ composer update intaro/twig-injection-bundle
Usage
- Add
{{ inject() }}calling in template:
{{ inject('twig.injection.event.name', { parameter1: 'some-value', parameter2: some_object }) }}
-
Prepare controller action which you want to render or template which you want to include.
-
Define Listener which will inject
includeorrendercalling:
<?php namespace Acme\DemoBundle\EventListener; use Intaro\TwigInjectionBundle\Event\TwigInjectEvent; use Intaro\TwigInjectionBundle\Event\TwigInjectRender; class TwigInjectionListener { public function onSomeEvent(TwigInjectEvent $event) { $parameters = $event->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); } }
- Register the listener:
services: acme_demo.twig_injection.listener: class: Acme\DemoBundle\EventListener\TwigInjectionListener tags: - { name: kernel.event_listener, event: twig.injection.event.name, method: onSomeEvent }