PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek