/** * @param \Twig_Environment $env * @param $context * @param TwigTemplateEvent $event * * @return string */ protected function render(\Twig_Environment $env, $context, TwigTemplateEvent $event) { $codes = $event->getCodes(); $compiled = ''; if (count($codes)) { foreach ($codes as $code) { if ($code instanceof TwigEventInclude) { $compiled .= $env->resolveTemplate($code->getTemplate())->render(array_replace_recursive($context, $code->getParameters())); continue; } if ($code instanceof TwigEventString) { $compiled .= $env->render($code->getTemplateString(), array_replace_recursive($context, $code->getParameters())); continue; } if ($code instanceof TwigEventRender) { $reference = new ControllerReference($code->getController(), $code->getAttributes(), $code->getQuery()); $compiled .= $this->fragment->render($reference, $code->getStrategy()); continue; } } } return $compiled; }
/** * @param TwigTemplateEvent $event */ public function onTemplateEvent(TwigTemplateEvent $event) { if ($event->getEventName() == 'test') { $event->addCode(new TwigEventString('hello {{ world }}', ['world' => 'World'])); } }