HomePHP Page 4 - Using a Template Processor Class in PHP 5
Going one step further: seeing the “TemplateProcessor” class in action - PHP
Welcome to part two of the series “Separating logic from presentation.” Comprised of three articles, this series walks you through the development of an extensible template processor in PHP 5, which you might find quite useful for separating the logic of your PHP applications from their visual presentation.
Having previously defined some of the most representative data sources, such as dynamic PHP files and MySQL result sets, all of them included within the corresponding array of input tags, the final step to demonstrate how the “TemplateProcessor” class can be used consists of putting together all the pieces in a single PHP script and showing the pertinent parsed (X)HTML output. Below is a snippet of code that shows the template processor in action:
try{ // include 'MySQL' class files require_once 'mysqlclass.php'; require_once 'resultclass.php'; // connect to MySQL $db=new MySQL(array ('host'=>'host','user'=>'user','password'=>'password', 'database'=>'database')); // run SQL query $result=$db->query('SELECT * FROM users'); // get query resource $queryResult=$result->getQueryResource(); // define input tags for template processor class $tags=array('title'=>'PHP 5 Template Processor','header'=>'header.php','navbar'=>array ('subnavigationbar1'=>'subnavbar1.php','navigationbar2'=> 'subnavbar2.php'), 'leftcontent'=>'leftcontent','maincontent'=>$queryResult, 'rightcontent'=> 'rightcontent','footer'=>'footer.php'); // instantiate a new template processor object $tpl=new TemplateProcessor($tags); // display compressed page echo $tpl->getHTML(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
As you can see, the above snippet demonstrates how to include all the input tags that I defined previously in one array, which is passed as argument to a template processor object. After parsing the example “default_template.htm” template file and displaying the processed web page, the output that I get on my browser is similar to this:
In the above screenshot, you can see how the corresponding placeholders have been replaced with real data coming from the previous sample PHP files, as well as from the MySQL dataset. In this case, I displayed only a couple of rows from a “users” database table, but I’m sure you get the idea of how result sets are processed by the “TemplateProcessor” class.
Also, with regard to the above image, the last thing worth noting is the recursive replacement of the “{navbar{subnavigationbar1}{subnavigationbar2}}" placeholders with the respective “subnavbar1.php” and “subnavbar2.php” PHP files. Even when the source code of this class is easy to grasp, you can see that its parsing features are really useful.
Now that I have demonstrated how the “TemplateProcessor” class is used with a variety of mixed data sources, feel free to tweak it and modify it, in order to meet your specific requirements. The experience is fun and instructive.
Bottom line
In thisarticle, you hopefully learned how to use the “TemplateProcessor” class, using a mixture of data sources, such as simple strings, dynamic PHP files and MySQL datasets. Also, you saw its recursive replacement capabilities in action, which can be very handy when working with complex template files.
Nevertheless, this series isn’t finished yet. In the last tutorial, I’ll show you how the base structure of the template processor can be used to develop a production-level class, which not only exposes the features that you saw before, but also implements the required logic for working with chunked caching and multiple template files. You won’t wan to miss it!