Антон Шевчук // Web-разработчик

Template engine HSTemplate

High Speed Template for PHP4/5 – it’s really very fast template because it’s not a “template language” wrote on PHP – it’s just PHP!

I created HSTemplate after reading this article (russian).

Detailed description

This package implements a template engine with output caching support.
It can assign templates files a name so they can be referenced by that name.
The class loads from a given directory template files which are actual HTML files with embedded PHP code.
It can assign to each template, variables which are stored as class variables.
The templates are processed by turning template variables into local variables and then include the template file scripts.
The results of processed templates can be cached to avoid subsequent template processing overhead.



Learn HSTemplate in 10 Minutes (Small tutorial)

Include the HSTemplate class library


Create instance of the HSTemplate class. For options “template_path” and “cache_path” use full path to directory.

/* HSTemplate initialization */
$options = array(
'template_path' => 'templates',
'cache_path'    => 'cache',
'debug'           => false,
$HSTemplate =& new HSTemplate($options);

Instantiate the HSTemplateDisplay object.

// index page
$DisplayIndex = & $HSTemplate->getDisplay('index');

Add Templates
Directory structure:

templates ('template_path')
`- index  ('display' name)
    |- header.html
    |- index.html
    `- footer.html


// add templates
$DisplayIndex->addTemplate('header', 'header.html');
$DisplayIndex->addTemplate('index' , 'index.html' );
$DisplayIndex->addTemplate('footer', 'footer.html');

Assign Variables
You can assign variable to ‘Global section’ or to ‘Display section’ or to ‘Template section’, priority:

  • Global section – all templates and all Display’s can use this is variables
  • Display section – all templates of selected Display can use this is variables
  • Template section – only selected template can use this is variables


// assign template variables
$DisplayIndex->assign('template', 'DISPLAY', 'index');
// assign display variables
$DisplayIndex->assign('display', 'DISPLAY');
// assign global variables (for all display)
$HSTemplate->assignGlobal('global', 'GLOBAL');

Templates is HTML files with inclusive PHP code

<h2>Test 1</h2>
Global   Variable: <?=$global?>
Display  Variable: <?=$display?>
Template Variable: <?=$template?>

Use Cache
Call method setCache for enable cache for curent Display:

  1. first argument is unique ID for cache (you can use $_SERVER[‘REQUEST_URI’])
  2. second – it’s cache lifetime – default value 3600 seconds (1hour)
$DisplayContent->setCache('test1', 3600);
if (!$DisplayContent->isCached()) {
    $DisplayContent->addTemplate('test1', 'test1.html');
    $DisplayContent->assign('time', date('H:i:s'), 'test1');


// display all non separated 'display'

Downloadlast versionHSTemplate

© Антон Шевчук 2007-2017