But the overhead of this templates engines is enormous, because of the all parsing envolved. So templates engines is not all about a sea of roses.
I have used Smarty for this kind of work for a long time and, in my opinion, is the best.
When i read this article i realized Karlus point of view. So based on this idea i have developed a small class that enables all funcionality that Smarty has (witch i use) but using the PHP natively as the parser.
It's a class with only 160 lines of code(without comments), so don't expect too much...
Features:
Using templates that are PHP files
Including templates inside templates
Caching with expired system in seconds or without expiration
Caching with id's
Plugin system, as simple as you can get
Discover the diferences with a simple template:
Smarty template (index.tpl) |
<html> <body> {include file="header.tpl"} Title: {$title} |
Smarty object creation (index.php) |
require_once('lib/smarty/Smarty.class.php'); $smarty =& new Smarty(); $smarty->template_dir = 'smarty/templates/'; $smarty->compile_dir = 'smarty/templates_c/'; $smarty->cache_dir = 'smarty/cache/'; $smarty->caching = 1; $smarty->assign('title', 'My Title'); $smarty->assign('intro', 'The intro paragraph.'); $smarty->assign('list', array('cat', 'dog', 'mouse')); $smarty->fetch('index.tpl'); |
My Template (index.tpl) |
<html> <body> <?=$this->includeTemplate("header.tpl");?> Title: <?=$title;?> <br /> List: <br /> <?php foreach ($list as $item):?> <?=$item;?> <br /> <?php endforeach; ?> <html> <body> |
My Template object creation (index.php) |
require_once('lib/Template.php'); $tpl =& new Template(); $tpl->setTemplatesPath('templates/'); $tpl->setCache(true); $tpl->setCachePath('cache/'); $tpl->setCacheId('nuno'); $tpl->setExpire(0); $tpl->assign('title', 'My Title'); $tpl->assign('intro', 'The intro paragraph.'); $tpl->assign('list', array('cat', 'dog', 'mouse')); $tpl->fetch('index.tpl'); |
Results
The time results shown below, were measured with average results to have a more accurate benchmark.The benchmarking was made in a Celeron 2400Mhz with 512Mb of RAM.
The Y axis corresponds to the average time in seconds per request and the X axis to the number of requests.
Download
You can download the source code here.
Besides, when the cache verification is triggered, I don't notice any delay that could be noticed "by humans". I confess I haven't investigated further to see how it reacts to a high volume of requests.