shapecode / twig-template-event-bundle
Possibility to add code in a twig template dynamically
Fund package maintenance!
nicklog
Liberapay
paypal.me/nloges
Installs: 10 562
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.2
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/event-dispatcher-contracts: ^2.0 || ^3.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/yaml: ^6.4 || ^7.0
- twig/twig: ^3.8
Requires (Dev)
- dg/bypass-finals: ^1.5
- doctrine/coding-standard: ^12.0
- icanhazstring/composer-unused: ^0.8
- maglnet/composer-require-checker: ^4.7
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.7
- symfony/var-dumper: ^7.0
README
Give you the possibility to add code in a twig template dynamically.
Install instructions
First you need to add shapecode/twig-template-event-bundle
to composer.json
:
composer require shapecode/twig-template-event-bundle
... or ...
{ "require": { "shapecode/twig-template-event-bundle": "~5.0" } }
If you dont use Symfony Flex you have to add ShapecodeTwigTemplateEventBundle
to your bundles.php
:
<?php // config/bundles.php return [ // ... Shapecode\Bundle\TwigTemplateEventBundle\ShapecodeTwigTemplateEventBundle::class => ['all' => true], ];
Now you can set events in twig templates:
{{ event('test') }}
And listen to them with an event listener:
services: # twig events Shapecode\Bundle\TwigTemplateEventBundle\EventListener\TestTwigEventListener: ~
<?php namespace Shapecode\Bundle\TwigTemplateEventBundle\EventListener; use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventString; use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventInclude; use Shapecode\Bundle\TwigTemplateEventBundle\Event\TwigTemplateEvent; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; #[AsEventListener] class TestTwigEventListener { public function __invoke(TwigTemplateEvent $event): void { if ($event->getEventName() == 'foo') { // to add a string $event->addCode( new TwigEventString( 'hello {{ world }}', [ 'world' => 'World' ], 10 // default is 0. The higher the number the later the code will be executed. The lower the number the earlier the code will be executed. ) ); } if ($event->getEventName() == 'bar') { // to include a twig template $event->addCode( new TwigEventInclude( '@App/Layout/Header/_search.html.twig', [ 'world' => 'World' ], ) ); } } }