lithium\net\http\Media::render PHP Méthode

render() public static méthode

Renders data (usually the result of a controller action) and generates a string representation of it, based on the type of expected output.
public static render ( object $response, mixed $data = null, array $options = [] ) : object
$response object 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.
$data mixed The data (usually an associative array) to be rendered in the response.
$options array Any options specific to the response being rendered, such as type information, keys (i.e. controller and action) used to generate template paths, etc.
Résultat object Returns a modified `Response` object with headers and body defined.
    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;
        });
    }

Usage Example

Exemple #1
0
 /**
  * This tests that setting custom paths and disabling layout
  * via `\lithium\net\http\Media::type()` is handled properly
  * by the default `\lithium\template\View` class and `File`
  * rendered adapter.
  */
 public function testMediaTypeViewRender()
 {
     Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => false, 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php'))));
     // testing no layout with a custom type template
     $response = new Response();
     $response->type('view-integration-test');
     Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile'));
     $this->assertEqual('This is a type test.', $response->body());
     // testing the template falls back to the html template
     $response = new Response();
     $response->type('view-integration-test');
     Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testFile'));
     $this->assertEqual('This is a test.', $response->body());
     // testing with a layout
     Media::type('view-integration-test', 'lithium/viewtest', array('view' => 'lithium\\template\\View', 'paths' => array('layout' => '{:library}/tests/mocks/template/view/adapters/testLayoutFile.html.php', 'template' => array('{:library}/tests/mocks/template/view/adapters/{:template}.{:type}.php', '{:library}/tests/mocks/template/view/adapters/{:template}.html.php'))));
     $response = new Response();
     $response->type('view-integration-test');
     Media::render($response, array(), array('layout' => true, 'library' => 'lithium', 'template' => 'testTypeFile'));
     $this->assertEqual("Layout top.\nThis is a type test.Layout bottom.", $response->body());
 }
All Usage Examples Of lithium\net\http\Media::render