PHP code example of shapecode / twig-template-event-bundle

1. Go to this page and download the library: Download shapecode/twig-template-event-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shapecode / twig-template-event-bundle example snippets



// config/bundles.php

return [
    // ...
    Shapecode\Bundle\TwigTemplateEventBundle\ShapecodeTwigTemplateEventBundle::class => ['all' => true],
];



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