Last Updated | | Ratings | | Unique User Downloads | | Download Rankings |
2024-01-09 (9 months ago) | | Not enough user ratings | | Total: 187 | | All time: 8,628 This week: 455 |
|
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. | |
|
|
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
-
PHXSLT is a XSLT Template Engine for PHP.
-
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.
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.