PHP
  Home arrow PHP arrow Page 3 - Developing an Extensible Template Proc...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this first article, you'll learn how to build the structure of the template...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway