/** * @param ViewModel $model * @param Response $response * @return string */ public function render(ViewModel $model, Response $response = null) { $body = ''; $helpers = $this->getHelpers(); $suffix = $this->getScriptSuffix(); // (0) Create the ROOT node $data = ViewData::cast($model->getData(), ['escape' => $helpers->get('escape')]); $this->injectHelpers($data); // (1) Render the view script $view_name = $model->getTemplate(); if ($view_name) { $view_script = $view_name . $suffix; $view_path = $this->templatePath($view_script); if (file_exists($view_path)) { $body = $this->renderScript($view_path, $data); } else { throw new Exception\RuntimeException("Script file ({$view_path}) not found"); } } // (2) Render the layout $layout_name = $this->modelLayout($model); if ($layout_name) { $layout_script = $layout_name . $suffix; $layout_path = $this->templatePath($layout_script); if (file_exists($layout_path)) { $data['content'] = $body; $body = $this->renderScript($layout_path, $data); } else { throw new Exception\RuntimeException("Layout file ({$layout_path}) not found"); } } // (3) Push if ($response) { $response->setBody($body); } return $body; }
/** * */ public function render(ViewModel $model, Response $response) { $body = ''; $engine = $this->getEngine(); // Create the ROOT node $data = $this->dataNode($model->getData(), false); // Get the layout name $layout_name = $model->getLayout(); $engine->defAlias('layout', $layout_name); // (1) Render the view script $view_name = $model->getTemplate(); if ($view_name) { $body = $engine->execute($view_name, $data, null); if ($body === false) { $error = $engine->getFirstError(); $this->logSubError($error); $info = isset($error['message']) ? '(' . $error['message'] . ')' : ''; throw new Exception\RuntimeException("View script [{$view_name}] could not be loaded {$info}"); } } // Push view errors (DEBUG) $data['view_errors'] = $engine->getErrors(); // (2) Render the layout if ($layout_name) { $data['content'] = $body; $layout = $engine->execute($layout_name, $data, null); if ($layout === false) { $error = $engine->getFirstError(); $this->logSubError($error); $info = isset($error['message']) ? '(' . $error['message'] . ')' : ''; throw new Exception\RuntimeException("Layout script [{$layout_name}] could not be loaded {$info}"); } $body = $layout; } $response->setBody($body); }