PHP
  Home arrow PHP arrow Page 3 - Developing an Extensible Template Processor in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing an Extensible Template Processor in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-05-02


    Table of Contents:
  • Developing an Extensible Template Processor in PHP 5
  • Getting started: defining the basic structure of the template processor
  • Assembling the template system: coding the "TemplateProcessor" class
  • Coding the workhorse of the class: defining the "processTemplate()" method
  • Getting the "TemplateProcessor" class completed: defining the remaining class methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing an Extensible Template Processor in PHP 5 - Assembling the template system: coding the "TemplateProcessor" class
    ( Page 3 of 5 )

    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()){
        if(count($tags)<1){
                throw new Exception('Invalid number of tags');
        }
        if($this->isCacheValid()){
            // read data from cache file
            $this->output=$this->readCache();
        }
        else{
            $this->tags=$tags;
            // read template file
            $this->output=file_get_contents($this->templateFile);
            // process template file
            $this->processTemplate($this->tags);
            // clean up empty tags
            $this->output=preg_replace("/{w}|}/",'',$this->output);
            // write compressed data to cache file
            $this->writeCache();
        }
        // send gzip encoding http header
        $this->sendEncodingHeader();
    }

    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
        if(file_exists($this->cacheFile)&&filemtime($this->cacheFile)
    >(time()-$this->expiry)){
            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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT