Home arrow PHP arrow Page 3 - Working with Multiple Template Files to Separate Logic from Presentation

Parsing multiple template files: redefining the “TemplateProcessor” class - PHP

Welcome to the last part of the series “Separating logic from presentation.” In three tutorials, this series teaches you how to develop an expandable template processor class using PHP 5, which exposes some useful features, such as recursive placeholder replacement, MySQL result sets processing, and parsing of dynamic PHP files, among others.

  1. Working with Multiple Template Files to Separate Logic from Presentation
  2. Setting up the basics of chunked caching: defining multiple template files
  3. Parsing multiple template files: redefining the “TemplateProcessor” class
  4. Putting the “TemplateProcessor” class to work: setting up a concrete example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
May 16, 2006

print this article



Before I list the corresponding source code of the improved “TemplateProcessor” class, let me first explain in a few words how it will work. Since this class must use all the template files previously defined, it will take up an additional array (aside from the array of input tags), which will contain the shortened name of the template file for being parsed, along with its cache expiration time, expressed in seconds.

If this explanation doesn’t ring any bells to you, don’t worry for the moment. When I set up a practical example, you’ll understand how this array will be used by the template processor.

Now, after a short introduction, please take a look at the signature of the modified “TemplateProcessor” class:

class TemplateProcessor{
    // declare data members
    private $output;
    private $rowTag='p';
    private $tags;
    private $templateFile;
    private $cacheFile;
    private $expiry;
    public function __construct($tags=array(),$pageSections=array
            throw new Exception('Invalid number of parameters for
template processor');
        foreach($pageSections as $key=>$expiry){
            // define template files
            // define cache files
            // assign cache expiration values
            // assign tags for replacement
            // initialize page outputs
        foreach($this->cacheFile as $key=>$cacheFile){
            // check if cache files are valid
                // read data from cache files
                // read template files
                // process template files
                // clean up empty tags
                // write crunched data to cache files
    // check validity of cache files
    private function isCacheValid($key){
        // determine if cache file is valid or not
            return true;
        return false;
    // process template file
    private function processTemplate($tags,$key){
        foreach($tags as $tag=>$data){
            // if data is array, traverse recursive array of tags
            // if data is a file, fetch processed file
            // if data is a MySQL result set, obtain a formatted
list of database rows
            elseif(@get_resource_type($data)=='mysql result'){
                    foreach($row as $col){
            // if data contains the '[code]' elimiter, parse data
as PHP code
    // process input file
    private function processFile($file){
          return $contents;
    // write compressed data to cache file
    private function writeCache($key){
            throw new Exception('Error writing data to cache
    // read compressed data from cache file
    private function readCache($key){
            throw new Exception('Error reading data from cache
        return $cacheContents;
    // return overall output
    public function getHTML(){
        foreach($this->output as $output){
        return $html;
    // return crunched output
    private function getCrunchedHTML($key){
        // crunch (X)HTML content & compress it with gzip
        // return compressed (X)HTML content
        return $this->output[$key];

As shown above, now the “TemplateProcessor” class looks a little more complex and intimidating, but essentially it implements the same logic that you learned in the previous articles. Since now the class must deal with multiple template and cache files, most of its properties have been redefined as arrays, a fact that you’ll clearly see if you analyze the signature of the constructor.

Notice how this method uses a “foreach” loop, in order to create the arrays corresponding to the cache and template files, the respective cache expiration times, and the parsed output of each section of the web page. Also, following the same approach, the method checks out, in sequence, the validity of each cache file and determines whether the template file in question should be parsed and saved later on, or the parsed contents should be directly fetched from the cache file.

In general terms, you can see that all the subsequent methods of the class use the same approach in working with different cache and template files, and most of them utilize array keys to handle each element in question. Therefore, if you already read the previous articles of the series, you shouldn’t have any problems understanding how the template processor works.

Well, before I proceed to coding a hands-on example, I’d like to point out one last thing: notice the way I redefined the private “getCrunchedHTML()” method, instead of compressing the parsed template files and saving them to cache. I deliberately defined the method like this, because I wanted to make the class flexible enough to include the data compression method that you most prefer. In this case, I decided only to remove new line characters from the parsed template files, but you can change this and use another technique for compressing data.

At this point, after explaining how the “TemplateProcessor” class implements chunked caching on multiple template files, it’s a good time to demonstrate its useful capabilities. Therefore, go ahead and read the next section, in order to see the template processor in action. It’s really worth it, trust me.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: