My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Tutorial  
Learn HSTemplate in 10 Minutes (Small tutorial)
Phase-Deploy, Phase-Implementation
Updated Aug 19, 2010 by AntonShe...@gmail.com

Include the HSTemplate class library

require_once('HSTemplate/HSTemplate.class.php');

Instantiate the HSTemplate object

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 (directory set as `template_path`)
   `- index  (by display name)
      |- header.html
      |- index.html
      `- footer.html

Example:

// 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:

  1. Global section
  2. Display section
  3. Template section

Example:

// 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

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

Use Cache

Call method setCache for enable cache for curent Display

  • first argument is unique ID for cache (you can use $_SERVER['REQUEST_URI'])
  • 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

// display all non separated 'display'
$HSTemplate->display();
Comment by thewis...@gmail.com, Nov 13, 2009

how to handle arrays ?

Comment by bodo....@gmx.net, Apr 29, 2010

I would like to enter an include to the content. Example: '<? include ("example.php"); ?>'

How does it look like if I want to include in the .php content file: '$site = array( "example" => " include 'inc/example.php'; "); ?>'

Thanks


Sign in to add a comment
Powered by Google Project Hosting