Revision: Tue, 23 Jun 2020 11:59:44 GMT

Views - Rendering Views

To render view, you must obtain an instance of Spiral\Views\ViewsInterface.

The component is available via prototyped property views.

Rendering

To render view in the controller or other service simply invoke the render method of ViewsInterface. The view name does not need to include extension or namespace (default to be used).

use Spiral\Views\ViewsInterface;

// ...

public function index(ViewsInterface $views)
{
    return $views->render('home');
}

To render the view with passed data, use the second array argument:

use Spiral\Views\ViewsInterface;

// ...

public function index(ViewsInterface $views)
{
    return $views->render('home', [
            'key' => 'value'
    ]);
}

Namespaces

To render view from specific namespace prepend it to view name using : separator:

return $views->render('namespace:home', [
    'key' => 'value'
]);

View Object

In some cases it might be more performant to cache the view in stateless component, obtain view object (Spiral\Views\ViewInterface) using method get of ViewsInterface:

$view = $views->get('home');

To render obtained view call render method:

return $view->render([
    'key' => 'value'
]);
Edit this page