HomePHP 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.
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.