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:
<?php
declare(strict_types=1);
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.
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?>
Note
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 }}
Note
The Stempler echo and directive syntax is similar to Laravel Blade.
The escape strategy will change based on where you echo your value. You can echo/embed your values inside script
tags:
<script>
var value = {{ $name }};
</script>
Note
Do not use quotes whiles doing so.
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 }}
@endforeach
Read more about available directives and custom directive creation here.