Home arrow PHP arrow Page 4 - Developing an Extensible Template Processor in PHP 5

Coding the workhorse of the class: defining the "processTemplate()" method - PHP

This is the first part of a three-part series that covers separating logic from presentation in PHP applications. In this article you will learn to develop a template system that is advanced enough to meet the requirements of a majority of applications.

TABLE OF CONTENTS:
  1. Developing an Extensible Template Processor in PHP 5
  2. Getting started: defining the basic structure of the template processor
  3. Assembling the template system: coding the "TemplateProcessor" class
  4. Coding the workhorse of the class: defining the "processTemplate()" method
  5. Getting the "TemplateProcessor" class completed: defining the remaining class methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 02, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: