PHP
  Home arrow PHP arrow Page 4 - Developing an Extensible Template Proc...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - Coding the workhorse of the class: defining the "processTemplate()" method


    (Page 4 of 5 )

    As I mentioned earlier, the "processTemplate()" method does most of the hard work, since it takes the arrays of tags passed as arguments, and replaces the pertinent placeholders with real data. However, this process isn't as simple as it sounds, since the placeholder replacement is performed recursively. Additionally, the method is capable of parsing dynamic PHP files, processing MySQL result sets and executing PHP code. Yes, I know you're curious about how this method looks, so here is its signature:

    private function processTemplate($tags){
        foreach($tags as $tag=>$data){
            // if data is array, traverse recursive array of tags
            if(is_array($data)){
                $this->output=preg_replace("/{$tag/",'',$this-
    >output);
                $this->processTemplate($data);
            }
            // 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.=' '.$col.' ';
                    }
                    $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=str_replace('{'.$tag.'}',$data,$this-
    >output);
        }
    }

    Indeed, the above method performs a few useful operations that I'll explain right now. As you can see, it takes the $tags array and iterates over it, in order to replace the corresponding placeholders with the values provided by these tags. As I mentioned previously, this is a recursive process (this means that nested placeholders and nested tags are allowed), and additionally, the method has the ability to parse PHP files, via the private "processFile()" method, in addition to processing MySQL data sets.

    This last feature implies iterating over database result sets, using the $this->rowTag property, which is used to separate each row of a database table and display them appropriately. Of course, here you have plenty of room to modify the method's source code and process MySQL datasets in a different way.

    Finally, the method utilizes the predefined "[code]" tag, which comes in handy for executing any string passed as a part of $tags array as PHP code. As you saw, the wealth of template processing features provided by this method is pretty useful. Again, if you happen to need more complex capabilities for processing template files, it's just a matter of expanding the functionality of this method.

    I'd like to end this section by listing the simple definition of the private "processFile()" method, tasked with parsing eventual PHP files that might be included within the incoming $tags array. Here's how this method looks:

    private function processFile($file){
        ob_start();
        include($file);
        $contents=ob_get_contents();
        ob_end_clean();
        return $contents;
    }

    Right, since the above method is actually easy to understand, I'd like to suggest you read the next few lines of this article. There you will see how the remaining methods of the "TemplateProcessor" are defined.

    More PHP Articles
    More By Alejandro Gervasio


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

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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