The spiral/views
component is available in Web bundle by default, to install it in alternative builds:
composer require spiral/views
Note
Please note that the spiral/framework >= 2.6 already includes this component.
To activate the component use bootloader Spiral\Bootloaders\Views\ViewsBootloader
:
[
// ...
Spiral\Bootloaders\Views\ViewsBootloader::class,
// ...
]
The component can work with multiple rendering engines (Twig, Stempler or native PHP templates) and store view files in multiple namespaces.
To change the default component configuration create and edit file app/config/views.php
:
<?php
declare(strict_types=1);
use Spiral\Views\Engine\Native\NativeEngine;
return [
'cache' => [
'enabled' => !env('DEBUG', false),
'directory' => directory('cache') . 'views'
],
'namespaces' => [
'default' => [
directory('views')
]
],
'dependencies' => [],
'engines' => [
NativeEngine::class
]
];
You can alter some of the component settings using bootloader Spiral\Bootloaders\Views\ViewsBootloader
.
To assign a new directory to view namespace:
namespace App\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Views\ViewsBootloader;
class AppBootloader extends Bootloader
{
public function boot(ViewsBootloader $views): void
{
$views->addDirectory('default', directory('root') . 'views');
}
}
To register a new view engine make sure to implement Spiral\Views\EngineInterface
:
namespace App\Bootloader;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Views\ViewsBootloader;
class AppBootloader extends Bootloader
{
public function boot(ViewsBootloader $views): void
{
$views->addEngine(MyEngine::class);
}
}
By default, view caching is turned off if the env variable DEBUG
is set to true. The cache files are stored in
runtime/cache/views
.
Run the console command views:reset
to delete view cache:
php app.php views:reset
To warmup cache before run views:compile
or configure
:
php app.php views:compile
Note
Always warmup view cache before switching worker pool under load.