@@ -611,21 +611,30 @@ PreRenderEvent
611
611
Subscribing to the ``PreRenderEvent `` gives the ability to modify
612
612
the twig template and twig variables before components are rendered::
613
613
614
+ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
614
615
use Symfony\UX\TwigComponent\EventListener\PreRenderEvent;
615
616
616
- public function preRenderListener(PreRenderEvent $event): void
617
+ class HookIntoTwigPreRenderSubscriber implements EventSubscriberInterface
617
618
{
618
- $event->getComponent(); // the component object
619
- $event->getTemplate(); // the twig template name that will be rendered
620
- $event->getVariables(); // the variables that will be available in the template
619
+ public function onPreRender(PreRenderEvent $event): void
620
+ {
621
+ $event->getComponent(); // the component object
622
+ $event->getTemplate(); // the twig template name that will be rendered
623
+ $event->getVariables(); // the variables that will be available in the template
624
+
625
+ $event->setTemplate('some_other_template.html.twig'); // change the template used
621
626
622
- $event->setTemplate('some_other_template.html.twig'); // change the template used
627
+ // manipulate the variables:
628
+ $variables = $event->getVariables();
629
+ $variables['custom'] = 'value';
623
630
624
- // manipulate the variables:
625
- $variables = $event->getVariables();
626
- $variables['custom'] = 'value';
631
+ $event->setVariables($variables); // {{ custom }} will be available in your template
632
+ }
627
633
628
- $event->setVariables($variables); // {{ custom }} will be available in your template
634
+ public static function getSubscribedEvents(): array
635
+ {
636
+ return [PreRenderEvent::class => 'onPreRender'];
637
+ }
629
638
}
630
639
631
640
Embedded Components
0 commit comments