Cake\Controller\Controller::render PHP Method

render() public method

Instantiates the correct view class, hands it its data, and uses it to render the view output.
public render ( string | null $view = null, string | null $layout = null ) : Response
$view string | null View to use for rendering
$layout string | null Layout to use
return Cake\Network\Response A response object containing the rendered view.
    public function render($view = null, $layout = null)
    {
        $builder = $this->viewBuilder();
        if (!$builder->templatePath()) {
            $builder->templatePath($this->_viewPath());
        }
        if (!empty($this->request->params['bare'])) {
            $builder->autoLayout(false);
        }
        $builder->className($this->viewClass);
        $this->autoRender = false;
        $event = $this->dispatchEvent('Controller.beforeRender');
        if ($event->result instanceof Response) {
            return $event->result;
        }
        if ($event->isStopped()) {
            return $this->response;
        }
        if ($builder->template() === null && isset($this->request->params['action'])) {
            $builder->template($this->request->params['action']);
        }
        $this->View = $this->createView();
        $this->response->body($this->View->render($view, $layout));
        return $this->response;
    }

Usage Example

 /**
  * Generate the response using the controller object.
  *
  * @param string $template The template to render.
  * @return void
  */
 protected function _outputMessage($template)
 {
     try {
         $this->controller->render($template);
         $event = new Event('Controller.shutdown', $this->controller);
         $this->controller->afterFilter($event);
         $this->controller->response->send();
     } catch (MissingViewException $e) {
         $attributes = $e->getAttributes();
         if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
             $this->_outputMessageSafe('error500');
         } else {
             $this->_outputMessage('error500');
         }
     } catch (\Exception $e) {
         $this->_outputMessageSafe('error500');
     }
 }
All Usage Examples Of Cake\Controller\Controller::render