Extends
lithium\core\Object
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 in `Controller::set()`. - `'head'`: If true, only renders the headers of the response, not the body. Defaults to `false`. - `'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 a `PostsController` object, if template is set to `'view'`, the template path would be `views/posts/view.html.php`. Defaults to the name of the action being rendered. The options specified here are merged with the values in the `Controller::$_render` property. You may refer to it for other options accepted by this method.
Returns
object Returns the `Response` object associated with this `Controller` instance.Source
public function render(array $options = array()) {
$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 = array(
'status' => null,
'location' => false,
'data' => null,
'head' => false,
'controller' => Inflector::underscore($name)
);
$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 + array(
'request' => $this->request
));
return ($this->response = $response ?: $this->response);
}