Revision: Wed, 27 Nov 2024 11:25:20 GMT
v3.13 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.14
Edit this page

Views — Plain PHP templating

You can use plain php files as your views without any compilation or caching layer. This engine is enabled by default and is available in the Web application skeleton.

Creating view

To create a plain PHP view, simply put a file with .php extension into the views directory. The template can be rendered by it's filename:

php
test.php
echo "hello world";
php
public function index(ViewsInterface $views): string
{
    return $views->render('test'); // no need to specify the extension
}

All the passed parameters will be available as PHP variables:

php
public function index(ViewsInterface $views): string
{
    return $views->render('test', ['name' => 'Antony']); 
}

Where test.php:

php
test.php
Hello , <?= $name ?>!

Container

You can access the container in your view files via $this->container:

php
test.php
Hello world, <?= $name ?>!

<?php dump($this->container->get(MyService::class)); ?>