Creating Templates

The SigmaPHP Template Engine provides a simple yet expressive way to build dynamic user interfaces using familiar HTML enhanced with powerful templating features.

Templates are regular HTML files using the .template.html extension. The engine parses these files and interprets special directives while keeping the syntax clean and readable. By default, templates are stored inside the app/Views directory, but this location can be customized through the application configuration.


/**
 * Views files path.
 */
'views_path' => env('VIEWS_PATH', 'app/Views'),
    

SigmaPHP Template supports template caching out of the box. When caching is enabled, the engine stores the compiled version of a template inside a cache file. On subsequent requests, the cached version is returned directly, improving performance significantly.

The engine automatically detects changes in the template file or in the data passed to it. If modifications are detected, the template is recompiled and the cache is refreshed with the updated result.


<?php

// The engine checks for a cached version first.
// If not found, the template will be compiled.
return $this->render('home');
    

Template caching is enabled by default to improve rendering performance. Cache files are stored inside the configured cache directory and can be disabled by setting the cache path to null.


/**
 * Cache files path.
 */
'cache_path' => env('CACHE_PATH', 'storage/cache'),
    

Caching is highly recommended in production environments to improve rendering performance. During development, however, the cache directory may grow over time. It is good practice to periodically clear its contents by removing all generated cache files.

On Linux systems, this can be done using the following command:


rm /path/to/cache/*
    

Creating a template is straightforward — simply add a new file inside the views directory. Templates may also be organized into sub-folders to keep large applications structured and maintainable.


// app/Views/index.template.html

<h1>{{ $itemList }}</h1>

<ul>
    {% for $item in $items %}
        <li>{{ $item->name }}</li>
    {% end_for %}
</ul>
    

For the complete syntax reference and a deeper understanding of how templates work, please refer to the Template Syntax.

Rendering templates from controllers is equally simple. The render($templateName, $variables, $code, $headers) method renders a template and immediately returns it as an HTTP response, while renderView($templateName, $variables) returns only the generated HTML string, which is useful when composing partial content.


public function dashboard()
{
    return $this->render(
        'dashboard',
        ['title' => 'Admin Panel', 'user' => 'Mohamed'],
        200,
        ['Cache-Control' => 'no-cache']
    );
}

public function partial()
{
    $content = $this->renderView(
        'partials.stats',
        ['visits' => 1500, 'sales' => 230]
    );

    return $this->response($content);
}
    
Back to top