Home arrow MySQL arrow Page 2 - Writing Your Own Template Caching Class In PHP

Writing Your Own Template Caching Class In PHP

Writing your own template caching class with PHP doesn't necessarily mean that you have to invent your own template tags, as Havard shows us in this article...

Author Info:
By: Havard Lindset
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
November 19, 2002
  1. · Writing Your Own Template Caching Class In PHP
  2. · How Is It Supposed To Work?
  3. · The Code Explained (contd.)
  4. · The Code Explained (contd.)
  5. · Real-World Examples
  6. · Conclusion

print this article

Writing Your Own Template Caching Class In PHP - How Is It Supposed To Work?
(Page 2 of 6 )

Before we start programming, I'll take you through a quick and easy example on how the templates and the template engine will work. Here's a simple template:

<title>Simple template example</title>


My first name is <?=$firstname?>, and my last name is <?=$lastname?>


Here's how we'll add the variables to it using the template class we're about to write:



$tpl = new Template();

$tpl->addVar('firstname', 'John');
$tpl->addVar('lastname', 'Doe');



Pretty easy, hey? The output of this example would be: "My first name is John, and my last name is Doe". As I explained above, the template is actually using normal PHP syntax for displaying the variables. This means that you don't have to learn another language for making templates.

I'll be using short tags throughout this tutorial. Short tags can be turned off in your php.ini file, so using them isn't always the best alternative. If you want maximum portability for your templates, you should use long tags:

<?php ?> instead of <? ?>

<?php echo $variable ?> instead of <?=$variable?>

Long tags cannot be turned off, so there won't be any problems with portability if you're using them. All right, it's time to get down to business and start programming.

Writing the Class
Let's start by declaring the class:


class Template {

var $_variables = array();
var $_caching = false;
var $_cacheDir = '/cache';
var $_cacheLifetime = 120;

We've now declared the class and four properties:
  • The $_variables property will be used for storing the variables that are added to templates.
  • The $_caching property is used for deciding whether or not templates should be saved to a cache.
  • The $_cacheDir property is used for telling the application where it's supposed to save the cache.
  • The $_cacheLifetime property is used for deciding how long the cached files are supposed to live.
We can now continue on to writing the methods. We'll start with addVar():

function addVar($name, $value)
$variables = &$this->_variables;
$variables[$name] = $value;

This method is pretty straightforward. We make a reference to $this->_variables by placing the & before it, and then add a new index to the array. If you haven't heard about references before, you can read about them in the php manual. Now that we have the addVar method, we can make the methods we'll use for displaying the templates:

function display($file, $id = false)
echo $this->fetch($file, $id);

This method probably doesn't make any sense to you right now, because we haven't declared the fetch method yet. But we'll just stop here for a bit, and I'll explain what the different variables are. The $file variable is the path to the template file you want to parse, and the $id variable is used for caching. You don't have to use the $id variable, but it's here for caching dynamic pages. Here's an example:

If you have a news-page that shows different news-items based on an article-id, you can use that article-id here to make a new cache file for each news-item. If you don't use the $id variable on pages like the one I described above, the cache will only save one file, which means that no matter what article-id you supply, you'll only get the one cached value. If you didn't understand this right now, don't worry. I have an example in this article where the $id variable is used.

function fetch($file, $id = false)
if ($this->_caching == true && $this->isCached($file, $id)) {
$output = $this->_getCache($file, $id);
} else {
$output = $this->_getOutput($file);

if ($this->_caching == true) {
$this->_addCache($output, $file, $id);

return isset($output) ? $output : false;

blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials