Luminova Framework

PHP Luminova: Template Rendering and View Object

Last updated: 2025-12-03 19:50:52

The View class renders templates with optional caching and layout inheritance, ideal for MVC/HMVC apps where you need structured and fast HTML output.

The Luminova's template View class is used to load and render templates content, capable of handling dynamic contents in MVC or HMVC application. It compiles your templates, presents them as HTML or other formats, and can cache the final output to speed up page loads.

Unlike the Content Response Class, which is for sending raw or API data, View focuses on rendering PHP, Smarty or Twig template contents (e.g, html, xml, js, etc.) and allows you to isolate template logics from main application. You can configure the template behavior to match your coding style.


Features

  1. Fast Rendering – Compiles and minifies templates automatically.
  2. Flexible Output – Supports templates that return HTML, arrays, objects, or scalars.
  3. Built-in Caching – Saves rendered templates for better performance.
  4. Highly Configurable – Adjust rendering modes via Template Rendering Configuration.

Template Rendering Modes

Luminova allows you render templates in two ways: Global Mode (the default) or Isolation Mode.You can switch modes in the Template View Configuration.

Global Mode

Global mode gives your template direct access to $this and self.That means the template can reach:

  • view-specific properties
  • public methods on the view class
  • application properties through $this->app
// /resources/Views/index.php

// View options
$this->_title;

// Application properties
$this->app->propertyName;

Use this when:

You want simple access to everything with no extra setup.Good for quick builds or templates that talk to the application often.


Isolation Mode

In isolation mode, the template cannot use $this or self.Instead, Luminova gives you a clean $self object with only what the view exposes.

// /resources/Views/index.php

// View options
$self->_title;

// Application properties
$self->app->propertyName;

Use this when:

You want a stricter boundary between the template and the rest of the app.Useful for cleaner structure, better security, and easier testing.


Smarty or Twig Templates

When you use Smarty or Twig, you can access the view object and the application instance through a global reference. Smarty uses $self, while Twig uses self.

{* /resources/Views/index.tpl *}

{* Smarty View Options *}
{{ $self.title }}

{* Smarty Application Properties *}
{{ $self.app.propertyName }}

{* Twig View Options *}
{{ self.title }}

{* Twig Application Properties *}
{{ self.app.propertyName }}

Note:Auto-prefixing of view options does not apply when using Smarty or Twig.If you wish to prefix options, you can do that in controller before rendering template


Template Scope Options

Luminova makes a set of variables automatically available inside every template.These variables give you details about the current view and help you control how the template behaves.

These options are split into two groups: Immutable (system-controlled) and Mutable (you can override them).


Immutable Options

These are set by the system and cannot be changed when rendering a view.

Note:$scope in the below examples refers to:

  • $this in Global Mode – Refers to the current view class instance. You can access view methods and application properties directly.
  • $self in Isolation Mode – Refers to an instance of Luminova\Template\Engines\Scope, which safely exposes only allowed view properties and the application object.

Note:Accessing $self in non-isolation mode will throw a RuntimeException.It is also recommended not to override $self inside the template file, as it may break the intended scope and guard functionality.


View Extension Type

Variable Name: viewType (string)

Represents the type of template content being rendered (e.g, html, xml etc..).

Examples:

// resources/Views/example.php

// When variable prefixing is enabled
echo $scope->_viewType; 

// When variable prefixing is disable
echo $scope->viewType;

Current Template

Variable Name: active (string)

The name of the current template file without the extension (e.g, index, about etc..).

Examples:

// resources/Views/example.php

// When variable prefixing is enabled
echo $scope->_active; 

// When variable prefixing is disable
echo $scope->active;

Assets

Variable Name: asset (string)

The base path to your public assets directory.

Use it as the starting point before adding the final file path.

Examples:

// resources/Views/example.php

// When variable prefixing is enabled
<img src="<?= $scope->_asset; ?>images/file.png">

// When variable prefixing is disable
<img src="<?= $scope->asset; ?>images/file.png">

// Result: /assets/images/file.png
{* Smarty *}
<img src="{{ $self.asset }}images/file.png">

{* Twig *}
<img src="{{ self.viewType }}images/file.png">

Also a global helper function available e.g, Luminova\Funcs\asset(...) or {{ asset(...) }} in frontend template engin.


Variable Name: href (string)

The base href used for building links.

Examples:

// resources/Views/example.php

// When variable prefixing is enabled
<a href="<?= $scope->_href; ?>about">About Us</a>

// When variable prefixing is disable
<a href="<?= $scope->href; ?>about">About Us</a>

// Result: /about
{* Smarty *}
<a href="{{ $self.href }}images/file.png">About Us</a>

{* Twig *}
<a href="{{ self.href }}images/file.png">About Us</a>

Also a global helper function available e.g, Luminova\Funcs\href(...) or {{ href(...) }} in frontend template engin.


Access Control

Constant Name: ALLOW_ACCESS (bool: true)

A constant you can use to block direct access to view files.

Example:

<?php
defined('ALLOW_ACCESS') || die('Access to file is not allowed');

// Template contents here

This ensure that only view class can render the template.


Mutable Options

These values can be overridden when rendering a template.

Template Cache

Variable Name: noCache (bool)

If true, the template will be skipped by the caching system if caching is enabled (see env('page.caching')).

Examples:

// /app/Controllers/Http/MyController.php

public function posts(): int 
{
    return $this->view('posts', [
        'noCache' => true
    ]);
}

Page Title

Variable Name: title (string)

The current page title.If not set, Luminova uses: "<active template>" – "<application name>"

Examples:

// /app/Controllers/Http/MyController.php

public function posts(): int 
{
    return $this->view('posts', [
        'title' => 'My Blog Post'
    ]);
}

Page Subtitle

Variable Name: subtitle (string)

The current page subtitle.If not set, it falls back to the title.

Examples:

// /app/Controllers/Http/MyController.php

public function posts(): int 
{
    return $this->view('posts', [
        'subtitle' => 'My Blog Post Contents'
    ]);
}

View Scope Options Access

Template options are extra values passed into a view during rendering.How you access them depends on your Template Variable Prefixing setting in the configuration.

Prefixing Enabled

When prefixing is set to TRUE, Luminova adds an underscore (_) before every option key:

$this->_fooOption;
$self->_fooOption;

This applies in both global and isolation modes.


Prefixing Disabled

When prefixing is set to FALSE, options appear as normal property names:

$this->fooOption;
$self->fooOption;

Be careful in isolation mode:Do not use self as an option name, since self is reserved for the isolated scope object.


Raw Options

With prefixing set to NULL, Luminova skips property injection and exposes the raw $options array directly:

$options['fooOption'];

This works the same way in both rendering modes.

You can also fetch values manually using the view helper methods:getOption() and getOptions().


Accessing Application Properties

Inside any template, you always have access to your main application instance through $app.

Both protected and public properties or methods in your App\Application class become available.

// /app/Application.php
namespace App;

use Luminova\Foundation\Core\Application as CoreApplication;

class Application extends CoreApplication
{
    protected ?Session $session = null;

    protected function onCreate(): void 
    {
        $this->session = new Session();
        $this->session->setStorage('users');
        $this->session->start();
    }
}

Then inside your view:

// /resources/Views/example.php

$this->app->session->isOnline();

// or in isolation mode
$self->app->session->isOnline();

For exporting properties in a controlled way, see the export() method in this template view documentation.


Other Variables

  • Variable Name: $_VIEW_FILEPATH (string) The absolute file path to the current template.
  • Variable Name: $_VIEW_TYPE (string) The template view content type.

Examples:

// resources/Views/example.php

echo $_VIEW_FILEPATH; 
// /path/application/resources/Views/example.php

echo $_VIEW_TYPE;
// html

Template Contents

Luminova templates support two main ways to provide content: direct output or returning content. How the content is processed depends on its type.

Outputting Content

This is the traditional way, the template directly outputs HTML or text. Use this when you want the template to immediately render to the browser or output buffer.

// resources/Views/example.php

<div>
    <p><?= $this->_user->name; ?></p>
    <a href="mailto:<?= $this->_user->email; ?>"><?= $this->_user->email; ?></a>
</div>

Returning Content

Templates can also return values instead of echoing them. Luminova automatically executes callables and formats the returned data before sending it to the client.

Supported return types and behavior:

TypeOutput Handling
Scalar (string, int, float, bool)Converted to string and sent directly.
ArrayJSON-encoded and sent with Content-Type: application/json.
ObjectJSON-encoded with Content-Type: application/json (unless it implements __toString() or toString()).
Stringable ObjectConverted to string via casting to (string) and sent.
SimpleXMLElementConverted to XML via asXML() with Content-Type: application/xml.
DOMDocumentConverted to XML via saveXML() with Content-Type: application/xml.
CallableExecuted, and its return value is processed recursively using the same rules.

Examples:

Returning an array.

// resources/Views/example.php

return [
    'name'  => $this->_user->name,
    'email' => $this->_user->email
];

Returning an object implementing __toString()

return new class($this->_user) {
    public function __construct(private User $user) {}
    public function __toString(): string { 
        return sprintf(
            'Contact %s, via %s',
            $this->user->name,
            $this->user->email
        ); 
    }
};

Returning a DOMDocument.

$dom = new DOMDocument();

$dom->loadXML('
    <user>
        <name>' . $this->_user->name . '</name>
        <email>' . $this->_user->email . '</email>
    </user>'
);

return $dom;

Returning a SimpleXMLElement → sent as XML

$xml = new SimpleXMLElement('<user/>');

$xml->addChild('name', $this->_user->name);
$xml->addChild('email', $this->_user->email);

return $xml;

Returning a callable.

return function(): array 
{
    return ['timestamp' => time()];
};

Note:

Returning arrays or objects automatically produces JSON output to the client.Returning scalars, Stringable objects, or XML types are rendered according to their type.Callables are executed before output.


Class Definition


Constants

Template Content Types

These constants define template content type identifiers used by the framework for operations such as caching, minification, and other processing. For more rendering support see Response Class Documentation

ConstantTypeValueDescription
HTMLstring'html'HTML templates or content.
JSONstring'json'JSON-formatted content.
TEXTstring'txt'Plain text content.
XMLstring'xml'XML-formatted content.
JSstring'js'JavaScript files or code blocks.
CSSstring'css'CSS stylesheets or inline styles.
RDFstring'rdf'RDF-formatted content for semantic data.
ATOMstring'atom'ATOM feed format.
RSSstring'rss'RSS feed format.
BINstring'bin'Binary string content format.

Note:

They are not the same as HTTP header Content-Type values, but are used internally for purposes such as caching, minification, and content processing.If no explicit HTTP header content type is set, these constants can also be used to determine the appropriate response header.


Properties

app

Holds the application instance.

public ?Luminova\Foundation\Core\Application $app = null

Does not create a circular reference with the view instance.

Examples:

$this->app; 
// or  
$self->app;

When to use:Use this property when you need access to application-level services, configuration, or methods directly from within a template without instantiating or passing the application object manually.Use export for private properties.


Methods

constructor

Initialize the View object.

This constructor sets up template configuration for view management, and loads environment-based options.

public __construct(?Luminova\Foundation\Core\Application $app): mixed

Parameters:

ParameterTypeDescription
$appCoreApplication|nullOptional application object.
If omitted, templates will not have access to the application instance via ($this->app or $self->app).

Note: You may not need to initialize this class manually as it is already available within the Luminova\Foundation\Core\Application class, which can be accessed inside controller classes.For global helper access, use Luminova\Funcs\view(...).


view

Sets the view template and its content type for rendering.

It resolves the template path and prepares it for rendering or later access.

Call this method to specify which view file to use, before any of these methods (render(), contents(), promise(), exists() or info()) are called.

Search Paths:

  • /resources/Views/ — MVC view directory.
  • /app/Modules/Views/ — HMVC root view directory.
  • /app/Modules/<Module>/Views/ — HMVC module-specific views.

Supported Content Types:

  • html, json, text|txt, xml, js, css, rdf, atom, rss

The $template must exclude file extensions (.php, .tpl, .twg, etc).For unsupported types, use response(...)->render(...) for manual handling.

final public view(string $template, string $type = self::HTML): self

Parameters:

ParameterTypeDescription
$templatestringView filename without extension (e.g., dashboard/index).
$typestringThe template content type (default: View::HTML).

Return Value:

self - Return instance of template view class.

Throws:

Example:

Usage in Controller:

// /app/Controllers/Http/FooController.php

// Render view and return status
$status = $this->tpl->view('profile', View::HTML)
    ->render(['id' => 1]);

// Render view and return content
$content = $this->tpl->view('settings', View::JSON)
    ->contents(['tab' => 'privacy']);

// Render view and return promise object
$promise = $this->tpl->view('invoice', View::HTML)
    ->promise(['orderId' => 101]);

// Return template filesystem information
$info = $this->tpl->view('index', View::HTML)
    ->info();

Direct Usage:

$view = new View(app: $application);

render

Render and immediately send the view output.

This method renders view content with an optional parameters to make globally availablewithin the template view file.

final public render(array<string,mixed> $options = [], int $status = 200): int

Parameters:

ParameterTypeDescription
$optionsarray<string,mixed>Additional parameters to pass in the template (available inside view).
$statusintThe HTTP status code (default: 200 OK).

Return Value:

int - Return one of the following status codes:

  • STATUS_SUCCESS if the view is handled successfully,
  • STATUS_SILENCE if failed, silently terminate without error page allowing you to manually handle the state.

Throws:

Example:

Display template view with options:

// /app/Controllers/Http/FooController.php

public function fooView(): int 
{
    return $this->tpl->view('name')
        ->render([...], 200);
}

contents

Render the view and return the output as a string.

This method renders selected template view and return the rendered contents string.

final public contents(array<string,mixed> $options = [], int $status = 200): ?string

Parameters:

ParameterTypeDescription
$optionsarray<string,mixed>Additional parameters to pass in the template (available inside view).
$statusintHTTP status code (default: 200 OK).

Return Value:

string|null - Return the compiled view contents or null if no content.

Throws:

Example:

Display your template view or send as an email:

// /app/Controllers/Http/MailController.php

use Luminova\Utility\Email\Mailer;

public function mailView(): int 
{
    $content = $this->tpl->view('name')
        ->contents(['foo' => 'bar'], 200);

    Mailer::to('[email protected]')->send($content);
}

promise

Return a promise that resolves with rendered view contents.

Renders the template view file and returns a promise that resolves with the rendered contents, or rejects if the template is missing or rendering fails.

final public promise(array<string,mixed> $options = [], int $status = 200): PromiseInterface

Parameters:

ParameterTypeDescription
$optionsarray<string,mixed>Additional parameters to pass in the template (available inside view).
$statusintHTTP status code (default: 200 OK).

Return Value:

Luminova\Interface\PromiseInterface - Return a promise that resolves with rendered view contents or rejects with an error.

Example:

Display your template view or send as an email:

// /app/Controllers/Http/FooController.php

public function fooView(): int 
{
    $content = $this->tpl->view('name')
        ->promise(['foo' => 'bar'])
        ->then(function(string $content, array $options) {
            Mailer::to('[email protected]')->send($content);
        })->catch(function(Exception $e) {
            logger('debug', $e->getMessage());
        });
}

info

Returns metadata about the specified view template.

Provides useful diagnostic and contextual information about a template file without rendering.

final public info(?string $key = null): mixed

Parameters:

ParameterTypeDescription
$keystring|nullOptional key to retrieve a specific value.

Return Value:

array<string,mixed>|mixed - Return the value for that key (null if not found), otherwise return metadata array.

Example:

$info = $this->tpl->view('dashboard')
    ->info();

// Get the modified
$modified = $this->tpl->view('dashboard')
    ->info('modified');

exists

Check if a template view file exists.

final public exists(): bool

Return Value:

bool - Return true if the template view file exists, false otherwise.

Example:

if($this->tpl->view('admin')->exists()){
    // Yes
}

export

Export a class instance or fully qualified name for later access in templates.

This allows registering services, classes, or any custom objectso that it can be accessed in the template via its alias.

Alternatively you can access public/protected properties and methods in application class directly using $this->app->property or $self->app->property.

final public export(object|string $target, ?string $alias = null, bool $shared = false): true

Parameters:

ParameterTypeDescription
$targetobject|stringThe class name, object instance to expose.
$aliasstring|nullOptional alias for reference (Defaults to class class/object name).
$sharedboolIf true and $target is class name, the same instance will be reused.

Return Value:

bool - Returns true if imported, otherwise throw error.

Throws:

Example:

// /app/Application.php

class Application extends CoreApplication
{
    protected function onCreate(): void 
    {
        $session = new Session();
        $session->setStorage('users');
        $session->start();

        // Export the session object for use in views
        $this->view->export($session, 'session');
    }
}

Note:If $target is not an object, it’s treated as a class name that will be instantiated later.


Accessing Exported Data in Views

Exported properties can be accessed using their $alias.If no alias is provided, the class basename is used automatically.

// /resources/Views/example.php
$this->session->isOnline();

// or when using isolated mode
$self->session->isOnline();

You can also retrieve exports through the helper method:

// /resources/Views/example.php
$this->getExport('session');
// or
$self->getExport('session');

header

Set a single response header.

This allows you to add a response header for template rendering,

final public header(string $key, mixed $value): self

Parameters:

ParameterTypeDescription
$keystringThe template response header key.
$valuemixedThe header value for key.

Return Value:

self - Return instance of template view class.

Example:

$this->tpl->header('Content-Type', 'application/json');

headers

Set multiple response headers at once.

This allows you to add a custom response headers for template rendering,

final public headers(array<string,mixed> $headers): self

Parameters:

ParameterTypeDescription
$headersarray<string,mixed>Associative array of headers key-pair.

Return Value:

self - Return instance of template view class.

Example:

$this->tpl->headers([
    'Cache-Control' => 'no-cache',
    'Content-Type' => 'text/html; charset=utf-8',
]);

setModule

Sets the HMVC module name for the current controller class.

This identifies the module that the controller belongs to, typically matching the folder name under app/Modules/<Module>.

For example, a controller located atapp/Modules/Blog/Controllers/PostController.php should use 'Blog' as the module name.

Use an empty string if the controller is not part of root module (i.e., global scope).

final public setModule(string $module = ''): self

Parameters:

ParameterTypeDescription
$modulestringThe module name (e.g., 'Blog'). Must not contain slashes or backslashes.

Return Value:

self - Return instance of template view class.

Throws:

Example:

// /app/Modules/Blog/Controllers/Http/ExampleController.php

namespace App\Modules\Blog\Controllers\Http;

use Luminova\Base\Controller;

class ExampleController extends Controller
{
    protected function onCreate(): void
    {
        $this->tpl->setModule('Blog');
    }

    //...
}

Note:

This method is intended for HMVC design pattern only, and should be called once in the controller’s __construct or onCreate method—before rendering any views.


setFolder

Sets the view subfolder used to locate view files within the application template view directory.

Valid base locations include:

  • resources/Views/ - MVC View directory.
  • app/Modules/Views/ - HMVC root view directory.
  • app/Modules/<Module>/Views/ - HMVC custom module view directory.
final public setFolder(string $path): self

Parameters:

ParameterTypeDescription
$pathstringSubfolder name to look for views.

Return Value:

self - Return instance of template view class.

Notes:

  • When used in a controller's onCreate or __construct, all views for that controller will be searched in this folder.
  • When used in the application's onCreate or __construct, it sets the default folder for all views.
  • When used in a controller method before rendering, only that method's view resolution is affected.

Example:

In this example, the setFolder method is used to direct the framework to look for template files in a custom directory (resources/Views/example/) for all views within a specific controller.

// /app/Controllers/Http/ExampleController.php

namespace App\Controllers\Http;

use Luminova\Base\Controller;

class ExampleController extends Controller
{
    protected function onCreate(): void
    {
        $this->tpl->setFolder('example');
    }

    public function show(): int
    {
        return $this->view('show');
    }

    public function foo(): int
    {
        // Overrides the default folder for the current method.
        $this->tpl->setFolder('example2');

        return $this->view('foo');
    }
}

setUriPathDepth

Set URI relative parent directories depth.

This method allows you to manually set how many parent directories (../) to prepend to asset and link paths.

It overrides Luminova's default auto-detection based on the current URI depth.Use it when working with nested views or custom routes that require explicit relative path control.

Example depth values:

  • 1../
  • 2../../
final public setUriPathDepth(int $depth): self

Parameters:

ParameterTypeDescription
$depthintNumber of ../ segments to prepend.

Return Value:

self - Return instance of template view class.

Useful when custom routing or nested views affect the correct relative path for assets.

Example:

Force asset paths to go up one directory:

// /app/Controllers/Http/ExampleController.php

namespace App\Controllers\Http;

use Luminova\Base\Controller;

class ExampleController extends Controller
{
    #[Route('app/account', methods: ['GET'])]
    protected function example(): void
    {
        // Override auto depth detection to use only one "../"
        $this->tpl->setUriPathDepth(1);

        return $this->view('account');
    }
}

In the corresponding view file:

// /resources/Views/account.php

echo Luminova\Funcs\asset('images/logo.png');
// OR
echo "{$this->_asset}images/logo.png";
// Generates: ../assets/images/logo.png

echo Luminova\Funcs\href('account/foo');
// OR
echo "{$this->_href}account/foo";
// Generates: ../account/foo

Normally, accessing https://example.com/app/account would trigger auto-detection of ../../ in development environment like XAMPP due to the URI depth (public/app/account).But since we explicitly set the depth to 1, only a single ../ is prepended to asset and link paths.


codeblock

HTML codeblock minification behavior.

This method allows you to configure whether HTML <code> blocks should be excluded from minificationand optionally display a copy button.

final public codeblock(bool $minify, bool $button = false): self

Parameters:

ParameterTypeDescription
$minifyboolWhether to skip minifying <code> blocks.
$buttonboolWhether to show a "copy" button inside code blocks (default: false).

Return Value:

self - Return instance of template view class.


noCaching

Exclude specific templates from being cached.

By default when page.caching = true is enabled in env, all view templates are cached and reused.This method allows you to exclude one or more templates name from caching it rendered content. Useful for selectively exclusion, ensuring that dynamic content is always up-to-date.

final public noCaching(string|string[] $template): self

Parameters:

ParameterTypeDescription
$templatestring|string[]A single template name or an array of template names to ignore from caching.

Return Value:

self - Return instance of template view class.

Example:

To disable caching for specific a view templates, example insert and edit will be excluded while allowing cache for other templates.

// /app/Application.php

namespace App;

use Luminova\Foundation\Core\Application as CoreApplication;

class Application extends CoreApplication
{
    protected function onCreate(): void
    {
        $this->view->noCaching(['insert', 'edit']);
    }
}

This can be configured either in your routes/<context>.php file, Application class or your view Controller class.The recommended approach is within the application onCreate or __construct method.


cacheOnly

Specify templates that should be cached exclusively.

This method allows you to explicitly add one or more templates that should only be cached.

Unlike noCaching method, the cacheOnly method allows you to specify which view templates should be exclusively cached while others are ignored. This is useful when you want to ensure that only certain views are cache and excluded others entirely.

final public cacheOnly(string|string[] $template): self

Parameters:

ParameterTypeDescription
$templatestring|string[]A single template name or an array of template names to allow for caching.

Return Value:

self - Return instance of template view class.

Example:

Enable caching for only home, about, and contact views

// /app/Application.php

namespace App;

use Luminova\Foundation\Core\Application as CoreApplication;

class Application extends CoreApplication
{
    protected function onCreate(): void 
    {
        $this->view->cacheOnly(['home', 'about', 'contact']);
    }
}

Applicable Usage:

If you have 100 views but only want to cache three of them (e.g,home, about, and contact). Using noCaching would mean listing all the other 97 views.Instead, use cacheOnly to list just the views you want cached. This way, only those three are cached, and everything else is automatically excluded.


cacheable

Enable or disable view caching globally at the controller or application level.

final public cacheable(bool $cacheable = true): self

Parameters:

ParameterTypeDescription
$cacheableboolWhether to allow view caching.

Return Value:

self - Return instance of template view class.

If used in a controller’s onCreate() or __construct(), all views under that controller will follow this rule.If used in the application class, applies to all views globally.Useful in api context to manually handle caching.

Example:

The cacheable method can be called in the controller's or application's onCreate or __construct method.

// /app/Application.php

namespace App\Controllers\Http;

use Luminova\Foundation\Core\Application as CoreApplication;

class Application extends CoreApplication
{
    protected function onCreate(): void
    {
        $this->view->cacheable(false);
    }
}

The cacheable method provides a straightforward way to control view caching by setting the caching status globally or for specific controllers, you can ensure that dynamic content remains current without being cached.


cache

Enables view response caching for reuse on future requests.

When called, this method marks the response to be cached. You can optionallyspecify a custom expiration time; otherwise, the default from the .env config will be used.

final public cache(\DateTimeInterface|int|null $expiry = null): self

Parameters:

ParameterTypeDescription
$expiry\DateTimeInterface|int|nullOptional cache expiration. Accepts:
- DateTimeInterface for specific expiration date/time,
- int for duration in seconds,
- null to use the default expiration from configuration.

Return Value:

self - Return instance of template view class.

Example:

Basic usage with conditional caching.

// /app/Controllers/Http/FooController.php

public function fooView(Luminova\Base\Model $model): int 
{
    $cache = $this->tpl->cache(60); // Cache for 60 seconds

    if ($cache->expired()) {
        $heavy = $model->doHeavyProcess();

        return $cache->view('foo')
            ->render(['data' => $heavy]);
    }

    return $cache->reuse(); // Reuse the previously cached response
}

delete

Deletes the cache entry for the current request view.

final public delete(?string $version = null): bool

Parameters:

ParameterTypeDescription
$versionstring|nullOptional. Specify the application version to delete (default: null).

Return Value:

bool - Return true if the cache entry was deleted; false otherwise.


clear

Clears all view cache entries.

final public clear(?string $version = null): int

Parameters:

ParameterTypeDescription
$versionstring|nullOptional. Specify the application version to clear (default: null).

Return Value:

int - Return the number of deleted cache entries.

Example:

Clear all cache entries for the current application version.

$total = $this->tpl->clear();

Clear cache entries for a specific application version.

$total = $this->tpl->clear('1.0.0');

expired

Check if page cache has expired.

Note: the expiration check we use the time used while saving cache.

final public expired(?string $type = self::HTML): bool

Parameters:

ParameterTypeDescription
$typestring|nullThe view content extension type (default: self::HTML).

Return Value:

bool - Returns true if cache doesn't exist or expired.

Throws:

Note:

Before calling this method, you must first call cache method instantiate it for use.

For the expiration check, we use the expiration set in env while saving cache, also don't worry about the cache key as it handled internally to save your precious time.


reuse

Reuse previously cached view content if available.

final public reuse(): int

Return Value:

int - Returns one of the following status codes:

  • STATUS_SUCCESS if cache was found and successfully reused,
  • STATUS_SILENCE if no valid cache was found — silent exit, allowing manual fallback logic.

Throws:

Example:

public function homepage(Luminova\Base\Model $model): int
{
    // Enable caching for 2 minutes
    $this->tpl->cache(120); 

    if ($this->tpl->reuse() === STATUS_SUCCESS) {
        // Cache hit, response already sent
        return STATUS_SUCCESS; 
    }

    $data = $model->getHomepageData();

    return $this->tpl->view('home')
        ->render(['data' => $data]);
}

onExpired

Conditionally renew cached view if expired, otherwise reuse it.

final public onExpired(Closure $onRenew, array $options = [], string $type = View::HTML): int

Parameters:

ParameterTypeDescription
$onRenew\ClosureCallback to execute if cache has expired. Should return STATUS_SUCCESS or STATUS_SILENCE.
$optionsarrayOptional options to pass to the $onRenew callback argument.
$typestringType of content to check cache for, (e.g. View::HTML, View::JSON).

Return Value:

int - Return the status code:

  • Status code from the callback if cache is expired or bypassed,
  • Otherwise, status code from reuse() if valid cache is used.

Example:

public function books(): int 
{
    return $this->tpl->cache(300)
        ->onExpired(function(array $options): int {
                return $this->view('books', [
                    'group' => $options['group'],
                    'books' => [
                        ['id' => 100, 'name' => 'Basic Programming'],
                        ['id' => 1001, 'name' => 'Advanced Programming']
                    ]
                ]);
            }, 
            ['group' => 'programming'] 
        );
}

redirect

Redirect to a different URI or route.

final public redirect(string $uri, int $status = 302): never

Parameters:

ParameterTypeDescription
$uristringThe target URI or route.
$statusintThe HTTP redirect status code (default: 302).

Generate a relative URI from the public root directory.

This method creates a relative path for routes or public assets (e.g., CSS, JS, images)starting from the controller’s public directory. In production, it returns a root-relative path.In development, it calculates the relative path based on URI segments.

final public static link(string $route = '', ?int $depth = null): string

Parameters:

ParameterTypeDescription
$routestringOptional route or file path to append after the base path.
$depthint|nullOptional depth to parent directory (used in development mode only).
If null, the method auto-detects the depth.

Return Value:

string - Return a relative or root-based URL to the file or route.

Example:

<link href="<?= $this->link('assets/css/main.css') ?>" rel="stylesheet">
<a href="<?= $this->link('about') ?>">About Us</a>

hasOption

Checks if a given name exists as a view option.

Useful for determining whether a value was set in the current view context via configuration options.

final public hasOption(string $name): bool

Parameters:

ParameterTypeDescription
$namestringThe option name to check.

Return Value:

bool - Returns true if the option exists, otherwise false.


isExported

Checks if a given property exists as an exported alias.

Useful for determining whether a value was made available to thecurrent view via the export() method.

final public isExported(string $name): bool

Parameters:

ParameterTypeDescription
$namestringThe export name or alias to check.

Return Value:

bool - Returns true if the export exists, otherwise false.


getExport

Retrieves a property exported from the application context.

final public getExport(string $name): mixed

Parameters:

ParameterTypeDescription
$namestringThe export property class name or alias used when exporting.

Return Value:

mixed|null - Return the export value, or null if not found.


getOption

Retrieves a value from the view's context options.

final public getOption(string $key): mixed

Parameters:

ParameterTypeDescription
$keystringThe option name.

Return Value:

mixed|null - Returns the option value if set; otherwise, checks for the key prefixed with _ and returns that value if found. Returns null if neither exists.

Note:

Supports fallback to prefixed keys for flexibility and backward compatibility.


getExports

Returns all exported application properties available to the view.

final public getExports(): array<string,mixed>

Return Value:

array<string,mixed> - Return an associative array of exported properties.


getOptions

Returns all view context options extracted for the view.

final public getOptions(): array<string,mixed>

Return Value:

array<string,mixed> - Return an associative array of view context options.


getTemplate

Get the template filename.

By default, returns the resolved template name (which may differ from the originalif a fallback like 404 was used).

Pass false to get the original template name exactly as specified in the controller.

final public getTemplate(bool $resolved = true): string

Parameters:

ParameterTypeDescription
$resolvedboolWhether to return the resolved filename (true) or the original (false).

Return Value:

string - Return the current view template file basename.


getStatusCode

Get the current HTTP status code.

Useful when you need to access the same status code that was used to render the template directly from inside the template itself.

final public getStatusCode(): string

Return Value:

int - Return the HTTP status code (e.g., 200, 404, 500).


toTitle

Converts the current view template name to a formatted page title.

Replaces underscores, dashes, hyphens, and commas with spaces, capitalizes words,and optionally appends the application name as a suffix.

final public toTitle(bool $suffix = false): string

Parameters:

ParameterTypeDescription
$suffixboolWhether to append the app name as a suffix (default: false).

Return Value:

string - Return the formatted page title.