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:
<html>
<head>
<title>Simple template example</title>
</head>
<body>
My first name is <?=$firstname?>, and my last name is <?=$lastname?>
</body>
</html> Here's how we'll add the variables to it using the template class we're about to write:
<?php
require_once('include/Template.php');
$tpl = new Template();
$tpl->addVar('firstname', 'John');
$tpl->addVar('lastname', 'Doe');
$tpl->display('templates/example.tpl.html');
?> 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:
<?php
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;
}Next: The Code Explained (contd.) >>
More MySQL Articles
More By Havard Lindset