Revision: Wed, 06 Dec 2023 18:31:31 GMT
v2.8 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.10
Edit this page

Stempler - Basics

Each view based on a template defined and stored inside app/views directory (or other configured via ViewsBootloader). The Stempler templates must have an extension .dark.php.

To render view template store file in app/views/welcome.dark.php.

hello world

Invoke in controller:



namespace App\Controller;

use Spiral\Prototype\Traits\PrototypeTrait;

class HomeController
    use PrototypeTrait;

    public function index(): string
        return $this->views->render('welcome');

You should see hello world on your screen.

Echo Value

To pass the value to the template, pass an array as the second argument of the render function.

return $this->views->render('welcome', [
    'name' => 'User'

Stempler templates support PHP underlying syntax (app/views/welcome.dark.php):

hello, <?=$name?>

You can always use fallback to PHP when needed. Attention, this way, does not prevent XSS injections!


Use alternative {{ $value }} syntax to echo the value with automatic escaping:

hello, {{ $name }}

The Stempler echo and directive syntax is similar to Laravel Blade.

Context-Aware escaping

The escape strategy will change based on where you echo your value. You can echo/embed your values inside script tags:

    var value = {{ $name }};

Do not use quotes whiles doing so.

Disable Escaping

To echo value as is (without anti-XSS) use alternative syntax {!! $value !!}:

{!! $value !!}


Besides classic echo constructions, the Stempler supports several Blade-like directives to control the business logic of your templates:

@foreach($values as $value)
    {{ $value }}

Read more about available directives and custom directive creation here.