shapecode / twig-template-event-bundle
Possibility to add code in a twig template dynamically
Fund package maintenance!
nicklog
Liberapay
paypal.me/nloges
Installs: 10 553
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ~7.2
- symfony/config: ~4.4|~5.0
- symfony/dependency-injection: ~4.4|~5.0
- symfony/event-dispatcher: ~4.4|~5.0
- symfony/event-dispatcher-contracts: ~1.0|~2.0
- symfony/framework-bundle: ~4.4|~5.0
- symfony/http-foundation: ~4.4|~5.0
- symfony/http-kernel: ~4.4|~5.0
- twig/twig: ~2.11|~3.0
Requires (Dev)
- dg/bypass-finals: ~1.1
- doctrine/coding-standard: ~8.0
- icanhazstring/composer-unused: ^0.7.3
- maglnet/composer-require-checker: ~2.0
- phpstan/phpstan: ~0.12
- phpstan/phpstan-deprecation-rules: ~0.12
- phpstan/phpstan-phpunit: ~0.12
- phpstan/phpstan-strict-rules: ~0.12
- phpunit/phpunit: ~9.0
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ~3.4
- symfony/var-dumper: ~5.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
:
{ "require": { "shapecode/twig-template-event-bundle": "~3.0" } }
Please note that dev-master
points to the latest release. If you want to use the latest development version please use dev-develop
. Of course you can also use an explicit version number, e.g., 1.0.*
.
You have to add ShapecodeTwigTemplateEventBundle
to your AppKernel.php
:
<?php // app/AppKernel.php //... class AppKernel extends Kernel { //... public function registerBundles() { $bundles = array( ... new Shapecode\Bundle\TwigTemplateEventBundle\ShapecodeTwigTemplateEventBundle(), ); //... return $bundles; } //... }
Now you can set events in twig templates:
{{ event('test') }}
And listen to them with an event listener:
// services.yml
services:
# twig events
Shapecode\Bundle\TwigTemplateEventBundle\EventListener\TestTwigEventListener:
tags:
- { name: kernel.event_listener, event: shapecode.twig_template.event, method: onTemplateEvent }
<?php namespace Shapecode\Bundle\TwigTemplateEventBundle\EventListener; use Shapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventString; use Shapecode\Bundle\TwigTemplateEventBundle\Event\TwigTemplateEvent; class TestTwigEventListener { public function onTemplateEvent(TwigTemplateEvent $event): void { if ($event->getEventName() == 'test') { $event->addCode(new TwigEventString('hello {{ world }}', array( 'world' => 'World' ))); } } }