PHP Classes

H2OTpl: Process and output PHP template scripts

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (9 months ago) RSS 2.0 feedNot enough user ratingsTotal: 187 All time: 8,628 This week: 455Up
Version License PHP version Categories
h2otpl 2.11GNU Lesser Genera...5.2PHP 5, Templates
Description 

Author

This class can process and output PHP template scripts

It can process a given template PHP script by including it and capturing its output.

The template can access the template variables using $this, as the template variables are class variables.

It supports all the constructs of PHP like loops, conditional sections, including external files, etc..

The template output can be compressed by removing HTML comments and whitespace. The final processed output can be cached in local files to avoid template reprocessing overhead.

Picture of Pierre-Henry Soria
  Performance   Level  
Name: Pierre-Henry Soria <contact>
Classes: 49 packages by
Country: United Kingdom
Innovation award
Innovation award
Nominee: 18x

Winner: 3x

Example

<?php
/**
 * @author Pierre-Henry Soria <ph7software@gmail.com>
 * @copyright (c) 2010-2014, Pierre-Henry Soria. All Rights Reserved.
 * @license LGPL Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
 * @link http://hizup.com
 */

if (version_compare(PHP_VERSION, '5.4.0', '<'))
{
    echo
'<p style="color:#FF0000;text-align:center">Oops! Your PHP version is <i>' . PHP_VERSION . '</i>. <b>H2OTpl Demo</b> is only compatible with PHP <i>5.4</i> or higher.</p>';
    exit;
}

if (
ini_get('short_open_tag') == 0)
{
    echo
'<p style="color:#FF0000;text-align:center"><b>Warning!</b> short_open_tag of your PHP configuration is Off (must be On!)<br /> This example cannot work without <b>short_open_tag</b>.</p>';
    exit;
}

define('SITE_URL', 'http://'.dirname($_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) .DIRECTORY_SEPARATOR);
include_once (
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libs/H2OTpl.class.php');
$oTpl = new H2OTpl;
$oTpl->setTplDir('templates'); // Set the templates directory
$oTpl->setCacheDir('cache'); // Set the cache directory
$oTpl->setTplExt('.tpl.php'); // Set a template extension
$oTpl->setCacheExt('.cache.php'); // Set a cache extension
$oTpl->setCaching(true); // Enable the cache
$oTpl->setCacheExpire(3600); // Set the caching time - 3600 = 1 hour
$oTpl->setHtmlCompress(true); // Set the HTML compress
// $oTpl->clearCache(); // Clear Cache

/* Disable automatic protection on variables, because we use HTML code in the variables. However, to protect other variable (such <title></title> tag in the example) it is still possible to use the escape() method. */
$oTpl->setAutoEscape(false);


// Create some variables...
$sName = 'Welcome To My WebSite';
$sH1Description = 'Hello World!';
$sDescription = 'Welcome to my site using <strong>H2OTpl</strong>: The <em>fastest</em> and <em>easiest</em> template engine using PHP!';

// Generate an array of authors and titles.
$aBookList = [
    [
       
'author' => 'Hernando de Soto',
       
'title' => 'The Mystery of Capitalism'
   
],
    [
       
'author' => 'Neal Stephenson',
       
'title' => 'Cryptonomicon'
   
],
    [
       
'author' => 'Milton Friedman',
       
'title' => 'Free to Choose'
   
]
];

// Assign values to the H2OTpl instance.
$oTpl->sBaseUrl = SITE_URL;
$oTpl->sTitle = $sName;
$oTpl->aBooks = $aBookList;
$oTpl->sTitle = $sName;
$oTpl->sH1 = $sH1Description;
$oTpl->sDesc = $sDescription;

// Display the template using the assigned values.
$oTpl->display('home');


Details

H2OTpl - PHP Template Engine

Description

“The simplest and best way to separate your HTML (template) code from your PHP code while coding in PHP!”

H2OTpl is an easy, fast and secure PHP template engine.

It was created for my H2O Engine (framework), but now I have decided to create a standalone version for people who don't want to use it with my H2O framework.

My other Web Template Engines

  1. PHXSLT is a XSLT Template Engine for PHP.
  2. PH7Tpl There is not standalone version available. pH7Tpl is integrated into my pH7Frameowork. It supports two different syntaxes. The first looks slightly to Smarty syntax and the second is inspired to XSLT/XML syntax but simplified and easier to use.

Server Requirements

H2OTpl class can work with PHP 5.2, but not the demo. So, I recommend to have PHP 5.4 or higher installed.

Author

Pierre-Henry Soria

Contact

You can contact me at: ph7software {AT} gmail {DOT} com

License

This script is under Lesser General Public License (LGPL); See the LICENSE.txt file for more information.


Screenshots (1)  
  • H2OTpl-Exemple
  Files folder image Files (13)  
File Role Description
Files folder imagedemo (1 file, 3 directories)
Accessible without login Plain text file CHANGES-LOG Data Auxiliary data
Plain text file H2OTpl.class.php Class Class source
Accessible without login Plain text file LICENSE.txt Lic. Documentation
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files (13)  /  demo  
File Role Description
Files folder imagecache (1 file)
Files folder imagelibs (1 file)
Files folder imagetemplates (1 file, 2 directories)
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (13)  /  demo  /  cache  
File Role Description
  Accessible without login Plain text file home.cache.php Output Cache output file

  Files folder image Files (13)  /  demo  /  libs  
File Role Description
  Accessible without login Plain text file H2OTpl.class.php Example Example script

  Files folder image Files (13)  /  demo  /  templates  
File Role Description
Files folder imageinc (2 files)
Files folder imagestyle (2 directories)
  Accessible without login Plain text file home.tpl.php Aux. Example script

  Files folder image Files (13)  /  demo  /  templates  /  inc  
File Role Description
  Accessible without login Plain text file footer.tpl.php Aux. Example script
  Accessible without login Plain text file header.tpl.php Aux. Example script

  Files folder image Files (13)  /  demo  /  templates  /  style  
File Role Description
Files folder imagecss (1 file)
Files folder imageimg (1 file)

  Files folder image Files (13)  /  demo  /  templates  /  style  /  css  
File Role Description
  Accessible without login Plain text file common.css Data Sample output

  Files folder image Files (13)  /  demo  /  templates  /  style  /  img  
File Role Description
  Accessible without login Image file HTML5.png Icon Icon image

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 92%
Total:187
This week:0
All time:8,628
This week:455Up