public static method

Renders data (usually the result of a controller action) and generates a string representation of it, based on the type of expected output.


  • object $response

    A Response object into which the operation will be rendered. The content of the render operation will be assigned to the $body property of the object, the 'Content-Type' header will be set accordingly, and it will be returned.

  • mixed $data

    The data (usually an associative array) to be rendered in the response.

  • array $options

    Any options specific to the response being rendered, such as type information, keys (i.e. controller and action) used to generate template paths, etc.



Returns a modified Response object with headers and body defined.


This method can be filtered.


	public static function render($response, $data = null, array $options = array()) {
		$params   = compact('response', 'data', 'options');
		$types    = static::_types();
		$handlers = static::handlers();
		$func     = __FUNCTION__;

		return static::_filter($func, $params, function($self, $params) use ($types, $handlers) {
			$defaults = array('encode' => null, 'template' => null, 'layout' => '', 'view' => null);
			$response = $params['response'];
			$data = $params['data'];
			$options = $params['options'] + array('type' => $response->type());

			$result = null;
			$type = $options['type'];

			if (!isset($handlers[$type])) {
				throw new MediaException("Unhandled media type `{$type}`.");
			$handler = $options + $handlers[$type] + $defaults;
			$filter = function($v) { return $v !== null; };
			$handler = array_filter($handler, $filter) + $handlers['default'] + $defaults;

			if (isset($types[$type])) {
				$mimeTypes = (array) $types[$type];

				$header  = current($mimeTypes);
				$header .= $response->encoding ? "; charset={$response->encoding}" : '';
				$response->headers('Content-Type', $header);
			$response->body($self::invokeMethod('_handle', array($handler, $data, $response)));

			return $response;