protected function renderTwigBlock(\Twig_Template $template, $blockName, $context = array()) { foreach ($template->getEnvironment()->getGlobals() as $key => $value) { if (!array_key_exists($key, $context)) { $context[$key] = $value; } } return $template->renderBlock($blockName, $context); }
/** * Renders a Twig block. * * see {@link https://github.com/twigphp/Twig/issues/676#issuecomment-15842093} * * @param \Twig_Template $template * @param string $block * @param array $context * * @return string * * @throws \Exception */ private function renderBlock(\Twig_Template $template, $block, array $context) { $context = $template->getEnvironment()->mergeGlobals($context); $level = ob_get_level(); ob_start(); try { $rendered = $template->renderBlock($block, $context); ob_end_clean(); return $rendered; } catch (\Exception $e) { while (ob_get_level() > $level) { ob_end_clean(); } throw $e; } }
/** * Adds a global to the template * * @param string $name * @param mixed $value */ public function addGlobalContext($name, $value) { $this->template->getEnvironment()->addGlobal($name, $value); }