PHP
  Home arrow PHP arrow Page 3 - Working with Multiple Template Files t...
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

Working with Multiple Template Files to Separate Logic from Presentation
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-05-16

    Table of Contents:
  • Working with Multiple Template Files to Separate Logic from Presentation
  • Setting up the basics of chunked caching: defining multiple template files
  • Parsing multiple template files: redefining the “TemplateProcessor” class
  • Putting the “TemplateProcessor” class to work: setting up a concrete example

  • 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


    Working with Multiple Template Files to Separate Logic from Presentation - Parsing multiple template files: redefining the “TemplateProcessor” class


    (Page 3 of 4 )

    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
    ()){
            if(count($tags)<1||count($pageSections)<1){
                throw new Exception('Invalid number of parameters for
    template processor');
            }
            foreach($pageSections as $key=>$expiry){
                // define template files
                $this->templateFile[$key]=$key.'_template.htm';
                // define cache files
                $this->cacheFile[$key]=md5($key).'.txt';
                // assign cache expiration values
                $this->expiry[$key]=$expiry;
                // assign tags for replacement
                $this->tags[$key]=$tags[$key];
                // initialize page outputs
                $this->output[$key]='';
            }
            foreach($this->cacheFile as $key=>$cacheFile){
                // check if cache files are valid
                if($this->isCacheValid($key)){
                    // read data from cache files
                    $this->output[$key]=$this->readCache($key);
                }
                else{
                    // read template files
                    $this->output[$key]=file_get_contents($this-
    >templateFile[$key]);
                    // process template files
                    $this->processTemplate($tags[$key],$key);
                    // clean up empty tags
                    $this->output[$key]=preg_replace("/{w}
    |}/",'',$this->output[$key]);
                    // write crunched data to cache files
                    $this->writeCache($key);
                }
            }
        }
        // check validity of cache files
        private function isCacheValid($key){
            // determine if cache file is valid or not
            if(file_exists($this->cacheFile[$key])&&filemtime($this-
    >cacheFile[$key])>(time()-$this->expiry[$key])){
                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(is_array($data)){
                    $this->output[$key]=preg_replace("/
    {$tag/",'',$this->output[$key]);
                    $this->processTemplate($data,$key);
                }
                // if data is a file, fetch processed file
                elseif(file_exists($data)){
                    $data=$this->processFile($data);
                }
                // if data is a MySQL result set, obtain a formatted
    list of database rows
                elseif(@get_resource_type($data)=='mysql result'){
                    $rows='';
                    while($row=mysql_fetch_row($data)){
                        $cols='';
                        foreach($row as $col){
                            $cols.='&nbsp;'.$col.'&nbsp;';
                        }
                        $rows.='<'.$this-
    >rowTag.'>'.$cols.'</'.$this->rowTag.'>';
                    }
                    $data=$rows;
                }
                // if data contains the '[code]' elimiter, parse data
    as PHP code
                elseif(substr($data,0,6)=='[code]'){
                    $data=eval(substr($data,6));
                }
                $this->output[$key]=str_replace('{'.$tag.'}',$data,$this->output[$key]);
            }
        }
        // process input file
        private function processFile($file){
              ob_start();
              include($file);
              $contents=ob_get_contents();
              ob_end_clean();
              return $contents;
        }
        // write compressed data to cache file
        private function writeCache($key){
            if(!$fp=fopen($this->cacheFile[$key],'w')){
                throw new Exception('Error writing data to cache
    file');
            }
            fwrite($fp,$this->getCrunchedHTML($key));
            fclose($fp);
        }
        // read compressed data from cache file
        private function readCache($key){
            if(!$cacheContents=file_get_contents($this->cacheFile
    [$key])){
                throw new Exception('Error reading data from cache
    file');
            }
            return $cacheContents;
        }
        // return overall output
        public function getHTML(){
            $html='';
            foreach($this->output as $output){
                $html.=$output;
            }
            return $html;
        }
        // return crunched output
        private function getCrunchedHTML($key){
            // crunch (X)HTML content & compress it with gzip
            $this->output[$key]=preg_replace("/(rn|n)/","",$this-
    >output[$key]);
            // 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


       · Over the course of this article, you'll learn how to build a powerful template...
       · Hi, first of all great article.I am having trouble with this line:// clean up...
       · Hey, thank you for the kind comments on this article. Regarding your question, try...
       · USE SMARTY (template engine) and you will bew happy!LOOK...
       · Thank you for your feedback regarding this article. I agree with you that Smarty is...
       · Parse error: parse error, unexpected '{' in...
       · Thank you for your comments on this article. Regarding your question, I used the...
       · My server is running php 4 and PHP 5, i had to name the file...
       · I'm glad to know you fixed out the problem.Thank you again for the kind words on...
       · Awesome tutorial ... I've found this series not only a great primer for learning how...
       · Thank you for your kind words on my PHP article. I really appreciate your comments....
       · First of all thanks for you usefull template engine.Second: I'm not a skilled PHP...
       · Thank you for commenting on my PHP article. Now, you can have a readable version of...
       · Thank you very much for your prompt answer.I also found it by myself 5 minutes...
       · You're welcome Massi. I'm glad to know the problem was solved...
       · Hello Alejandro,thanks for the good tutorial, I have this code on...
       · Hi Roy,Thank you again for the comments on my PHP article. Concerning your...
     

       

    PHP ARTICLES

    - 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
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





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