To render a view, you must obtain an instance of Spiral\Views\ViewsInterface
.
Note
The component is available via the prototyped propertyviews
.
To render a view in the controller or other service, simply invoke the render
method of ViewsInterface
. The view name
does not need to include an extension or namespace (default to be used).
use Spiral\Views\ViewsInterface;
// ...
public function index(ViewsInterface $views): string
{
return $views->render('home');
}
To render the view with the passed data, use the second array argument:
use Spiral\Views\ViewsInterface;
// ...
public function index(ViewsInterface $views): string
{
return $views->render('home', [
'key' => 'value'
]);
}
To render a view from a specific namespace, prepend it to the view name using :
separator:
return $views->render('namespace:home', [
'key' => 'value'
]);
In some cases it might be more performant to cache the view in a stateless component, obtain the view
object (Spiral\Views\ViewInterface
) using the method get
of ViewsInterface
:
$view = $views->get('home');
To render obtained view call render
method:
return $view->render([
'key' => 'value'
]);
You can add global variables that will be available in all views. There are several ways to add a global variable.
You can use the configuration file app/config/views.php
.
return [
'globalVariables' => [
'some_var' => env('SOME_VALUE'),
'other_var' => 'other_value'
]
];
Or you can use Spiral\Views\GlobalVariablesInterface
.
namespace App\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Views\GlobalVariablesInterface ;
use Spiral\Boot\EnvironmentInterface;
class AppBootloader extends Bootloader
{
public function boot(GlobalVariablesInterface $vars, EnvironmentInterface $env): void
{
$vars->set('some_var', $env->get('SOME_VALUE'));
$vars->set('other_var', 'other_value');
}
}
After that, you can use the variable in the view.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?=$some_var?>
<?=$other_var?>
</body>
</html>
Event | Description |
---|---|
Spiral\Views\Event\ViewNotFound | The Event will be fired when the view is not found. |