Home arrow PHP arrow Page 3 - Developing an Extensible Template Processor in PHP 5

Assembling the template system: coding the "TemplateProcessor" class - PHP

This is the first part of a three-part series that covers separating logic from presentation in PHP applications. In this article you will learn to develop a template system that is advanced enough to meet the requirements of a majority of applications.

  1. Developing an Extensible Template Processor in PHP 5
  2. Getting started: defining the basic structure of the template processor
  3. Assembling the template system: coding the "TemplateProcessor" class
  4. Coding the workhorse of the class: defining the "processTemplate()" method
  5. Getting the "TemplateProcessor" class completed: defining the remaining class methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 02, 2006

print this article



In order to start building the "TemplateProcessor" class, the first thing I'll do is show the definition of its constructor. The signature for this class method is shown below. 

public function __construct($tags=array()){
            throw new Exception('Invalid number of tags');
        // read data from cache file
        // read template file
        // process template file
        // clean up empty tags
        // write compressed data to cache file
    // send gzip encoding http header

As shown above, the constructor performs some crucial tasks, after checking the validity of the $tags array passed as a parameter. Essentially, this method first determines whether the corresponding cache file is valid, and in accordance with this, reads the parsed web page from the cache.

On the other hand, if the cache has expired, then the method reads the template file in question, then processes it by the private "processTemplate()" method, and finally saves the parsed page to the cache file. Naturally, this operation is performed by the private "writeCache()" method.

The ending line of the constructor calls up the "sendEncodingHeader()" method, in order to tell the browser that data will be sent encoded. Short and understandable, right?

After explaining the logic implemented by the constructor, I'll move on and show you the definition of the "isCacheValid()" method, which, as I said before, determines the validity of the respective cache file. Please look at the following method:

private function isCacheValid(){
    // determine if cache file is valid or not
        return true;
    return false;

In this case, the definition of the above method is very simple. In short, what it does is check whether the cached contents are valid or not, based on a time expiration caching trigger. If the cache file is found to be valid, the method returns true. Otherwise, the returned value will be false.

Now that you learned how some useful methods of the "TemplateProcessor" class work, go ahead and read the next section of the article. That's where I'll show you the definition of the "processTemplate()" method, which is actually the core engine of the class.

>>> 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: