Bolt\Render::render PHP Method

render() public method

Render a template, possibly store it in cache. Or, if applicable, return the cached result.
public render ( string $templateFile, array $context = [], array $globals = [] ) : TemplateResponse
$templateFile string Template file name
$context array Context variables
$globals array Global variables
return Bolt\Response\TemplateResponse
    public function render($templateFile, $context = [], $globals = [])
    {
        $this->app['stopwatch']->start('bolt.render', 'template');
        /** @var Environment $twig */
        $twig = $this->app[$this->twigKey];
        /** @var Template $template */
        $template = $twig->loadTemplate($templateFile);
        foreach ($globals as $name => $value) {
            $twig->addGlobal($name, $value);
        }
        $html = $template->render($context);
        $response = new TemplateResponse($html);
        $response->setTemplate($template)->setContext($context)->setGlobals($globals);
        $this->app['stopwatch']->stop('bolt.render');
        return $response;
    }

Usage Example

Example #1
0
 /**
  * Render the not found page if on frontend and http exception
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if (!$event->getException() instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) {
         return;
     }
     $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]);
     if (!$content instanceof Content || empty($content->id)) {
         return;
     }
     $template = $this->templateChooser->record($content);
     $response = $this->render->render($template, $content->getTemplateContext());
     $event->setResponse($response);
 }
All Usage Examples Of Bolt\Render::render