Framework provides deep integration with Twig Template engine including access to IoC scopes, i18n integration and caching.
To install Twig:
$ composer require spiral/twig-bridge
The extension can be enabled using Spiral\Twig\Bootloader\TwigBootloader
.
protected const LOAD = [
// ...
Spiral\Bootloader\Views\ViewsBootloader::class,
Spiral\Bootloader\Views\TranslatedCacheBootloader::class, // keep localized views in separate cache files
Spiral\Twig\Bootloader\TwigBootloader::class,
// ...
];
You can add any custom extension to Twig via addExtension
method of TwigBootloader
:
class TwigExtensionBootloader extends Bootloader
{
public function boot(TwigBootloader $twig)
{
$twig->addExtension(MyExtension::class);
// custom options
$twig->setOption('name', 'value');
}
}
You can use twig views immediately. Create view with .twig
extension in app/views
directory.
Hello, {{ name }}!
You can use this view without an extension in your controllers:
public function index()
{
return $this->views->render('filename', ['name' => 'User']);
}
You can freely use twig
include
andextends
directives.
To access the value from the IoC scope:
Hello, {{ name }}!
{{ get("Spiral\\Http\\Request\\InputManager").attribute('csrfToken') }}