PHP code example of toropyga / templates

1. Go to this page and download the library: Download toropyga/templates library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

toropyga / templates example snippets




use Toropyga\TemplateConfig;
use Toropyga\Templates;

$templates = Templates::fromConfig(new TemplateConfig(
    rootPath: __DIR__,
    templatePath: 'templates',
    style: 'default',
    cachePath: 'var/cache/templates'
));

$templates->assign(['user_name' => 'Alex']);

echo $templates->render('hello.html');

$html = $templates->output('hello.html', true);
$templates->output('hello.html');

$templates->setEscapeHtml(false);

$templates = new Templates('templates', 'default', 'cache', true);

use Toropyga\TemplateException;

try {
    echo $templates->render('hello.html');
} catch (TemplateException $error) {
    // Handle the error in the application.
}

$templates->setThrowExceptions(false);
$result = $templates->output('missing.html');
$errors = $templates->getErrors();

$templates->clearCache();

    $templates->setAllowPhp(true);
    

    $templates->clearCache();
    

$templates = new Templates('templates', 'default', 'cache');
$templates->assign(['page' => $page]);
$templates->output('page.html');

$config = new Toropyga\TemplateConfig(
    rootPath: defined('ROOT_PATH') ? (string) constant('ROOT_PATH') : __DIR__,
    templatePath: defined('TMPL_DIR') ? (string) constant('TMPL_DIR') : 'templates',
    style: defined('TMPL_STYLE') ? (string) constant('TMPL_STYLE') : 'default',
    cachePath: defined('TMPL_CACHE') ? (string) constant('TMPL_CACHE') : 'cache',
    sitePath: defined('SITE_PATH') ? (string) constant('SITE_PATH') : ''
);

$templates = Toropyga\Templates::fromConfig($config);
bash
php tests/smoke.php         # compilation pipeline: vars, |raw, uard
php tests/legacy_smoke.php  # legacy constructor + multi-instance behavior
php tests/legacy_isolation.php # legacy DOCUMENT_ROOT-based instance isolation
php tests/cache_smoke.php   # asset-method consolidation + cache hash reuse