shapecode / twig-template-event-bundle
Possibility to add code in a twig template dynamically
Fund package maintenance!
nicklog
Liberapay
paypal.me/nloges
Installs: 10 656
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 1
Forks: 1
Open Issues: 0
Type:symfony-bundle
pkg:composer/shapecode/twig-template-event-bundle
Requires
- php: ^8.4
- symfony/config: ^7.4 || ^8.0
- symfony/dependency-injection: ^7.4 || ^8.0
- symfony/event-dispatcher: ^7.4 || ^8.0
- symfony/event-dispatcher-contracts: ^3.6
- symfony/http-foundation: ^7.4 || ^8.0
- symfony/http-kernel: ^7.4 || ^8.0
- symfony/service-contracts: ^3.6
- twig/twig: ^3.22
Requires (Dev)
- dg/bypass-finals: ^1.9
- doctrine/coding-standard: ^14.0
- phpstan/phpstan: ^2.1.33
- phpstan/phpstan-deprecation-rules: ^2.0.3
- phpstan/phpstan-phpunit: ^2.0.10
- phpstan/phpstan-strict-rules: ^2.0.7
- phpunit/phpunit: ^12.5.3
- roave/security-advisories: dev-master
- shapecode/twig-string-loader: ^2.0
- shipmonk/composer-dependency-analyser: ^1.8.4
- squizlabs/php_codesniffer: ^4.0.1
- symfony/var-dumper: ^8.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' ], ) ); } } }