Revision: Fri, 17 May 2024 13:03:33 GMT
v1.0 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.12
Edit this page

Twig Views

The Spiral application bundle includes Twig rendering engine by default.


You can skip extension when no view conflicts are expected and/or use default namespace notation:


Twig engine follows same cache settings as other engines, see views config.


TwigEngine defined in views config:

'twig'   => [
    'class'      => Engines\TwigEngine::class,
    'extension'  => 'twig',
    'options'    => [
        'auto_reload' => true

    * Modifiers applied to imported or extended view source before it's getting parsed by
    * HtmlTemplater, every modifier has to implement ModifierInterface and as result view
    * name, namespace and filename are available for it. Modifiers is the best to connect
    * custom syntax processors (for example Laravel's Blade).
    'modifiers'  => [
        //Automatically replaces [[string]] with their translations

        //Mounts view environment variables using @{name} pattern.


    * Here you define list of extensions to be mounted into twig engine, every extension
    * class will be resolved using container so you can use constructor dependencies.
    'extensions' => [
        //Provides access to dump() and spiral() functions inside twig templates

Use options array to pass values into Twig_Environment. View translation using [[]] and environment values @{name} are allowed in twig templates due to TranslateProcessor and EnvironmentProcessor.

Twig Extension

Use extensions section or twig config to define Twig_Extension list (resolved via container).

Default SpiralExtension extension provides access to IoC scope and spiral function:

    window.csrfToken = "{{ spiral('request').getAttribute('csrfToken') }}";

dump function is available as well.

Direct Access

Access twig directly in your code and bootloaders:

$this->views->engine('twig')->getTwig(); //Twig_Environment