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 = []) {
		$params = compact('response', 'data', 'options');

		return Filters::run(get_called_class(), __FUNCTION__, $params, function($params) {
			$types = static::_types();
			$handlers = static::handlers();

			$defaults = ['encode' => null, 'template' => null, 'layout' => '', 'view' => null];
			$response = $params['response'];
			$data = $params['data'];
			$options = $params['options'] + ['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(static::_handle($handler, $data, $response));

			return $response;