Using a Template Processor Class in PHP 5 - Going one step further: seeing the “TemplateProcessor” class in action (
Page 4 of 4 )
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 this article, 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!
| | Discuss Using a Template Processor Class in PHP 5 | | | | | | | In this tutorial, the template processor I developed during the previous article is... | | | | | | Hi Alejandro,
I have read many of your articles and have found all of them an... | | | | | | Hi there,
Thank you for your comments on my PHP article, as well as your kind... | | | | | | Alejandro, That one doesn't work either?? I know practically nothing about regular... | | | | | | Hello again,
Sorry to hear about the syntax error again, but I used the class... | | | | | | I got the same syntax error above - and like many find regular expressions rather... | | | | | | 2 with reference to the above error - the problem is not in the regular expression... | | | | | | First off, I'd like to thank you for commenting on my PHP article. And lastly, with... | | | | | | just another note, the compressed html regex needs to escape for the line breaks... | | | | | | Thank you for posting some improvements on the template parser class, since they're... | | | | | | i tried the suggestion but it did not work so i edited it to (\r\n|\n) and it... | | | | | | Thanks alejandro for the wonderful code, it took me a whole night of anylizing where... | | | | | | Thank you for commenting on my PHP article. I see you modified the source code of... | | | | | | Thank you for commenting on my PHP article. The correct regexp is the one you used,... | | | | | | >>> Post your comment now! | | | | | |
|
 |