Revision: Thu, 26 Sep 2024 15:47:52 GMT
v2.14 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.14
Edit this page

Views - Plain PHP

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

Create view

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

php
<?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
Hello , <?= $name ?>!

Container

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

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

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