HomePHP Building a Template Parser Class with PHP, Part II
Building a Template Parser Class with PHP, Part II
In part one of this two part article series, you learned how to build a simple template parser class in PHP. In this second article, you will learn how to add caching capabilities to the class.
Welcome to the second part of the article "Building a Template Parser class with PHP." In the first part of the article, I’ve hopefully demonstrated that building a simple template parser class is not as intimidating as it seems. Given the flexibility and power that PHP offers, it’s possible to implement efficient applications without scratching our heads.
Since the original version of the class was quite simple, I felt strongly tempted to expand it, adding a little more functionality. So, I thought that incorporating caching capabilities to the class, without incurring time-consuming processes, was worth considering. In this second part, I’m going to show how to improve the original class, by adding a simple caching mechanism, with little modifications. So, let’s get started.
Refreshing memories: a quick look at the original class
Before we get into the process of tweaking the class code a bit, it would be good to remind ourselves how the original version looked, in order to establish a starting point for implementing the new caching capabilities. Here’s the full code for the template parser class:
class templateParser {
var $output;
function templateParser($templateFile='default_template.htm'){
(file_exists($templateFile))?$this- >output=file_get_contents($templateFile):die('Error:Template file '.$templateFile.' not found');
die('Error: No tags were provided for replacement');
}
}
function parseFile($file){
ob_start();
include($file);
$content=ob_get_contents();
ob_end_clean();
return $content;
}
function display(){
return $this->output;
}
}
Since we’re talking about a template class, there must be a template file for proper processing. Therefore, let’s show the code for the template file used, as it was originally conceived:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Now, things are beginning to make sense, and I can briefly explain the underlying logic of the class. To begin with, the class constructor accepts the template file as the only parameter for being processed. After checking whether the file really exists, it simply assigns the file contents to the $output variable, which is storing the page, as it’s being generated. Then, the workhorse method of the class, that is, "parseTemplate()", takes the incoming $tags array for replacing all of the placeholders present in the template file, along with the corresponding data, stored in the mentioned $tags array. Doing so, the placeholders {header}, {navbar}, {leftcontent}, and so forth, will be substituted will real data, coming from regular variables or files.
As you can see, the method is calling internally to "parseFile()," which takes care of retrieving the contents from any file that might be included as part of the $tags array, parsing static or dynamic content. This is extremely handy for processing data from PHP variables, including any kind of dynamic information. Stick with me, because the explanation is almost done.
Once the "parseTemplate()" method has performed the replacement process, the remaining task is a breeze. The class exposes the "display()" method for getting the finished version of the page, which is stored in the $output variable, to be displayed in the browser, or processed in another way, according to the application’s needs.
Well, maybe this is not shocking news, but if we feed the class with a correct template file, we obtain a finished page populated with the corresponding data. This is a simple and effective way to give websites a consistent look with no big hassles. The final class implementation might be something similar to this:
<?php
// include the class
require_once('template.php');
// instantiate a new template Parser object
$tp=&new templateParser('template.htm');
// define parameters for the class
$tags=array('title'=>'You are seeing the template parser class in action!','header'=>'header.php','navbar'=>'navigation bar.php','leftcontent'=>'leftcontent.php', 'maincontent'=> 'maincontent.php','rightcontent'=>'rightcontent.php', 'footer'=> 'footer.php');
// parse template file
$tp->parseTemplate($tags);
// display generated page
echo $tp->display();
?>
Okay, if you read the first part of this article, the above code is pretty familiar, but for those just coming in for this sequel, the previous explanation was well spent. Now, having briefly explained the core logic behind the class, let’s get ready to add a pinch of caching capabilities. Just keep reading to find out more.