Luminova Framework

PHP Luminova: Template View Isolation and the $self Variable

Last updated: 2026-12-03 03:43:04

Access view data, exported objects, and layouts through `$self` in isolated Luminova PHP templates.

Template isolation changes the scope in which Luminova runs a PHP view. The framework includes the view without binding it to a Luminova\Template\View $this object and provides the $self as the view scope. Through $self, the template can read view options and exported objects, call available view methods, and access the layout or application when attached.

$self is a PHP variable supplied at render time. Use syntax such as $self->title or $self->layout. Reserve isolation for trusted PHP templates. They still execute PHP and can call functions available to the application.


Enable Isolation

Set these properties in the existing App\Config\Template class in app/Config/Template.php.

public string $templateEngine = 'default';
public bool $templateIsolation = true;
public ?bool $variablePrefixing = true;

The examples use Luminova’s built-in PHP template engine. The isolation setting applies to the rendered view. Controller actions still use $this. Template Configuration · Template View Rendering

Isolation Modes

Use $self only in controller-rendered templates or when accessing the view object and application object in isolation mode within layout files.

Example: Isolation Mode Enabled

When App\Config\Template->templateIsolation = true:

$self->app->appMethod();
$self->_title;
$self->_href;
$self->_asset;
$self->getOptions();

Isolation Mode Disabled

When App\Config\Template->templateIsolation = false:

$this->app->appMethod();
$this->_title;
$this->_href;
$this->_asset;
$this->getOptions();

Note:When defining layout sections, always use the $this keyword inside layout files, regardless of whether template isolation is enabled.


Pass Data from a Controller

Pass display data as the second argument to the controller’s view() method.

namespace App\Controllers\Http;

use Luminova\Attributes\Route;
use Luminova\Base\Http\AbstractController;

final class CatalogController extends AbstractController
{
    #[Route('/catalog', methods: ['GET'])]
    public function catalog(): int
    {
        return $this->view('catalog', [
            'title' => 'Product catalog',
            'item-count' => 3,
        ]);
    }
}

In resources/Views/catalog.php, read the options through $self. With prefixing enabled, Luminova adds an underscore and replaces hyphens in option names with underscores.

<?php use function Luminova\Funcs\escape; ?>

<h1><?= escape($self->_title) ?></h1>
<p><?= (int) $self->_item_count ?> items</p>

Option Names

$variablePrefixingOption passed as ['title' => 'Product catalog']
trueRead $self->_title.
falseRead $self->title.
nullRead $options['title'] from the raw options array in the view.

With prefixing disabled, supplying a view option named self in isolation mode raises a runtime exception. Keep the framework-supplied $self variable intact throughout rendering.


Expose an Object to the View

Use a view option for data and export() for an object the view needs to call. For example, place this code before rendering in the controller action.

$this->tpl->export(new class {
    public function itemLabel(int $count): string
    {
        return $count === 1 ? '1 item' : "{$count} items";
    }
}, 'catalogLabels');

Read the exported object by its alias in the template. Variable prefixing applies to view option names. Export aliases are read as registered.

<?php use function Luminova\Funcs\escape; ?>

<p><?= escape($self->catalogLabels->itemLabel((int) $self->_item_count)) ?></p>

Use distinct names for view options and export aliases so a view option cannot take precedence over the export. Template View Rendering


Access Layouts and Application Services

  • Layouts. In a controller-rendered PHP view, use $self->layout when enablePhpTemplateLayout is enabled. A layout file or partial executed by the layout engine has its own scope. PHP Template Composition
  • Application. Use $self->app when an application has been attached to the view. For a template that only needs a few values or an object, pass options or export the object from the controller.
  • View methods. Call public view methods through $self, such as $self->getOption('_title') with prefixing enabled.

When a view reports that $this is unavailable, check $templateIsolation and update the view’s context access to $self. A property or method call on $self raises a logic exception when isolation is disabled. In that mode, a controller-rendered PHP view accesses its view context through $this.