/** * Render a template * * Call this method within a GET, POST, PUT, DELETE, NOT FOUND, or ERROR * callable to render a template whose output is appended to the * current HTTP response body. How the template is rendered is * delegated to the current View. * * @param string $template The name of the template passed into the View::render method * @param array $data Associative array of data made available to the View * @param int $status The HTTP response status code to use (Optional) * @return void */ public function render($template, $data = array(), $status = null) { if (!is_null($status)) { $this->response->status($status); } $this->view->setTemplatesDirectory($this->config('templates.path')); $this->view->appendData($data); $this->view->display($template); }
/** * Render a template * * Call this method within a GET, POST, PUT, DELETE, NOT FOUND, or ERROR * callable to render a template whose output is appended to the * current HTTP response body. How the template is rendered is * delegated to the current View. * * @param string $template The name of the template passed into the View::render method * @param array $data Associative array of data made available to the View * @param int $status The HTTP response status code to use (Optional) * @return void */ public function render($template, $data = array(), $status = null) { $templatesPath = $this->config('templates.path'); //Legacy support if (is_null($templatesPath)) { $templatesPath = $this->config('templates_dir'); } $this->view->setTemplatesDirectory($templatesPath); if (!is_null($status)) { $this->response->status($status); } $this->view->appendData($data); $this->view->display($template); }
/** * Get and/or set the View * * This method declares the View to be used by the Slim application. * If the argument is a string, Slim will instantiate a new object * of the same class. If the argument is an instance of View or a subclass * of View, Slim will use the argument as the View. * * If a View already exists and this method is called to create a * new View, data already set in the existing View will be * transferred to the new View. * * @param string|Slim_View $viewClass The name of a Slim_View class; * An instance of Slim_View; * @return Slim_View */ public function view($viewClass = null) { if (!is_null($viewClass)) { $existingData = is_null($this->view) ? array() : $this->view->getData(); if ($viewClass instanceof Slim_View) { $this->view = $viewClass; } else { $this->view = new $viewClass(); } $this->view->appendData($existingData); $this->view->setTemplatesDirectory($this->config('templates.path')); } return $this->view; }
<?php $partial = new Slim_View(); $partial->setTemplatesDirectory('partials'); function renderPartial($template, $data) { global $partial; $partial->setData($data); return $partial->render($template); }