Code that works: AbsTemplate

As a web developer you need code to reuse in other projects, otherwise you’ll find yourself doing the same thing over and over again. This is bad for you and bad for your business if you have one or just work as a freelancer, and wikipedia summarizes it like this:

Code reuse is the idea that a partial or complete computer program written at one time can be, should be, or is being used in another program written at a later time. The reuse of programming code is a common technique which attempts to save time and energy by reducing redundant work.

So I’ll begin my “Code that works” series with AbsTemplate. This is a very simple to use and configure class that I wrote back in 2009 inspired by the Smarty template engine.

This class somehow emulates Smarty and provides caching utility beside template parsing. By using the default configuration you only need to provide the path to the templates directory. If caching is desired then the path to the cache directory is required also, otherwise you can omit it.

To get the best of this class you should load and instantiate it in a global configuration file and then include it in all your pages so it will be available inside all your templates files.

/*!
| @ Default configuration file
|------------------------------------------------------------------
| ! This file must be located in the root directory of your website
*/
	//! Show all errors
	error_reporting(-1);
 
	//! Load the AbsTemplate class
	require 'class.AbsTemplate.php';
 
	//! Set the path to the root directory
	$root = realpath(dirname(__FILE__)).'/';
 
	//! [Required] Set the path to the templates directory
	$tplDir = $root.'templates/';
 
	//! Set the path to the cache directory.
	//! Only required if you want to cache your templates,
	//! otherwise you can omit it.
	$cacheDir = $root.'cache/';
 
	//! [Optional] Set the left delimiter.
	$leftDelimiter = '{';
 
	//! [Optional] Set the right delimiter.
	$rightDelimiter = '}';
 
	//! [Optional] Set the default expire time. In hours.
	$defaultCacheLifetime = 1;
 
	//! Instantiate the Caching Template class
	$tpl = new AbsTemplate( $tplDir, $cacheDir, $leftDelimiter, $rightDelimiter, $defaultCacheLifetime );

The left delimiter and the right delimiter will be used to wrap variables in your template files so they can be replaced by their assigned values when the template is being parsed.

//! Inside your template file
 
	<h2> {title} </h2>

The default cache lifetime is set by default to 0, so the cache is disabled. To enable caching you should first set the path to the cache directory. If a different cache expire length is desired you can set it when you call the GetTemplate method.

	//! Set cache for 2 hours
	$header = $tpl->GetTemplate('header.php', 2);

This class doesn’t force you to set a precise file extension for your templates files. You are free to use any extension you like:

	// get header template
	$header = $tpl->GetTemplate('header.php',2);
 
	// get main content template
	$content = $tpl->GetTemplate('content.tpl');
 
	// get sidebar template
	$sidebar = $tpl->GetTemplate('sidebar.tpl.php');
 
	// get footer template
	$footer = $tpl->GetTemplate('footer.php',5);

So how would a default page look like?

<?php
	require 'class.AbsTemplate.php';
 
// Instantiate the Template class
	$tpl = new AbsTemplate('templates','cache');
 
// Set some variables to use in the template files
	$tpl->SetVar('TITLE', 'The default usage of AbsTemplate');
 
	$copy = '&copy;';
	$tpl->SetVar('copy', 'Copyright '.$copy.' notice here');
 
// Get templates
	$header = $tpl->GetTemplate('header.php');
	$content = $tpl->GetTemplate('content.php');
	$sidebar = $tpl->GetTemplate('sidebar.php');
	$footer = $tpl->GetTemplate('footer.php');
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title><?php echo $tpl->GetVar('TITLE');?></title>
</head>
<body>
<div id="layout">
 
	<div id="header"><?php echo $header;?></div>
 
	<div id="body">
		<div id="content"><?php echo $content; ?></div>
 
		<div id="sidebar"><?php echo $sidebar; ?></div>
	</div>
 
	<div id="footer"><?php echo $footer; ?></div>
 
</div>	
</body>
</html>

I successfully use this class in combination with other classes (that will follow in this series) in every project that I see them fit, and I hope you’ll find them as useful as I do :)

Download archive: AbsTemplate.

Your opinions are welcome!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>