lithium\action\Controller::render()
Uses results (typically coming from a controller action) to generate content and headers for
a Response
object.
Parameters
-
array
$options
An array of options, as follows:
'data'
: An associative array of variables to be assigned to the template. These are merged on top of any variables set inController::set()
.'head'
: If true, only renders the headers of the response, not the body. Defaults tofalse
.'template'
: The name of a template, which usually matches the name of the action. By default, this template is looked for in the views directory of the current controller, i.e. given aPostsController
object, if template is set to'view'
, the template path would beviews/posts/view.html.php
. Defaults to the name of the action being rendered. The options specified here are merged with the values in theController::$_render
property. You may refer to it for other options accepted by this method.
Returns
objectReturns the Response
object associated with this Controller
instance.
Source
public function render(array $options = []) {
$media = $this->_classes['media'];
$class = get_class($this);
$name = preg_replace('/Controller$/', '', substr($class, strrpos($class, '\\') + 1));
$key = key($options);
if (isset($options['data'])) {
$this->set($options['data']);
unset($options['data']);
}
$defaults = [
'status' => null,
'location' => false,
'data' => null,
'head' => false,
'controller' => Inflector::underscore($name),
'library' => Libraries::get($class)
];
$options += $this->_render + $defaults;
if ($key && $media::type($key)) {
$options['type'] = $key;
$this->set($options[$key]);
unset($options[$key]);
}
$this->_render['hasRendered'] = true;
$this->response->type($options['type']);
$this->response->status($options['status']);
$this->response->headers('Location', $options['location']);
if ($options['head']) {
return;
}
$response = $media::render($this->response, $this->_render['data'], $options + [
'request' => $this->request
]);
return ($this->response = $response ?: $this->response);
}