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
Esempio n. 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);
 }
Esempio n. 2
0
 /**
  * Handle errors thrown in the application.
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     // Log the error message
     $message = $exception->getMessage();
     $this->logger->critical($message, ['event' => 'exception', 'exception' => $exception]);
     if ($exception instanceof HttpExceptionInterface && !Zone::isBackend($event->getRequest())) {
         $message = "The page could not be found, and there is no 'notfound' set in 'config.yml'. Sorry about that.";
     }
     $context = ['class' => get_class($exception), 'message' => $message, 'code' => $exception->getCode(), 'trace' => $this->getSafeTrace($exception)];
     // Note: This uses the template from app/theme_defaults. Not app/view/twig.
     $response = $this->render->render('error.twig', ['context' => $context]);
     $event->setResponse($response);
 }
Esempio n. 3
0
 /**
  * Insert a snippet of Javascript to fetch the actual widget's contents.
  *
  * @param WidgetAssetInterface $widget
  * @param Response             $response
  */
 protected function addDeferredJavaScript(WidgetAssetInterface $widget, Response $response)
 {
     if ($this->deferAdded) {
         return;
     }
     $javaScript = $this->render->render('widgetjavascript.twig', ['widget' => $widget]);
     $snippet = (new Snippet())->setLocation(Target::AFTER_BODY_JS)->setCallback((string) $javaScript);
     $this->deferAdded = true;
     $this->injector->inject($snippet, Target::AFTER_BODY_JS, $response);
 }
Esempio n. 4
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;
     }
     // If $notFoundPage is referencing a template, render it and be done.
     if ($this->render->hasTemplate($this->notFoundPage)) {
         $response = $this->render->render($this->notFoundPage);
         $event->setResponse($response);
         return;
     }
     // Next try for referencing DB content.
     $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);
 }