Skip to content

Repository files navigation

Shapecode - Twig Template Event Bundle

paypal

PHP VersionLatest Stable VersionLatest Unstable VersionTotal DownloadsMonthly DownloadsDaily DownloadsLicense

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.phpreturn [
// ...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 eventsShapecode\Bundle\TwigTemplateEventBundle\EventListener\TestTwigEventListener: ~
<?phpnamespaceShapecode\Bundle\TwigTemplateEventBundle\EventListener;
useShapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventString;
useShapecode\Bundle\TwigTemplateEventBundle\Event\Code\TwigEventInclude;
useShapecode\Bundle\TwigTemplateEventBundle\Event\TwigTemplateEvent;
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener]
class TestTwigEventListener
{
publicfunction__invoke(TwigTemplateEvent$event): void
{
if ($event->getEventName() == 'foo') {
// to add a string$event->addCode(
newTwigEventString(
'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(
newTwigEventInclude(
'@App/Layout/Header/_search.html.twig', [
'world' => 'World'
],
)
);
}
}
}

About

No description, website, or topics provided.

Resources

Stars

10 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages