Login

Using a Template Processor Class in PHP 5

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.

Introduction

As you’ll know (and assuming that you already took a look at the first article of the series), in the first tutorial I explained how to build in a PHP 5-driven template processor class. It exposed a few handy methods aimed at parsing the respective template file passed as an argument, by replacing its placeholders with actual data provided as an array structure.

If the above concepts are quite familiar to you, then you’ll recall that I provided my template processor class with certain interesting features, such as the ability to recursively replace placeholders, parse dynamic PHP files, process MySQL result sets, and run PHP code included as part of the input tags array. Of course, as I said before, the class can be easily expanded, either by defining more methods or modifying the existing ones, in order to fit the particular requirements of more demanding applications. As in most cases, deciding when and how to expand the template processor class will depend on the size and complexity of the Web project where the class will be included.

Now, bringing our attention to this second installment of the series, I’ll show you how to use the template processor class by coding some sample codes, so you can have an exact idea of how to include this class either in your own PHP projects or as part of a larger shared application.

Having defined the goals for this article, let’s leap forward and begin learning more about how to take advantage of this PHP 5 template processor class. Let’s go!

{mospagebreak title=Getting started using the “TemplateProcessor” class: a quick look at its definition}

Before I proceed to demonstrate how to use the “TemplateProcessor” class, I need to remind you of how it looks, so it can be fresh in your mind. That said, here’s the corresponding definition for the class, as I wrote it originally in the first article:

I suppose at this point, after listing the “TemplateProcessor” class, you’re ready to see an illustrative hands-on example, in order to learn how you can use it for parsing your template files. Want to learn how this will be achieved? Please, read the next few lines.

Right, I think the best way to demonstrate the functionality of my “TemplateProcessor” class is simply by feeding it a considerable variety of data, which will be eventually parsed and displayed within the corresponding template file, after performing the replacement of placeholders. Considering this, I’ll first define two dynamic files, “header.php” and “footer.php” respectively, which will compose the header and footer sections of a sample web page. Take a look at these two basic PHP files:

<?php echo ‘<h1>This is the header section and was generated at the following time ‘.date(‘H:i.s’).'</h1>’; ?>

<?php echo ‘<h1>This is the footer section and was generated at the following time ‘.date(‘H:i.s’).'</h1>’; ?>

Now, after listing the above PHP files, I’ll include a simple MySQL result set as part of the input of the “TemplateProcessor” class. To do this, I’ll use two additional MySQL wrapping classes, which are listed below:

Now that you saw the source code of the two MySQL wrapping classes listed above, which I’ll utilize for fetching a trivial MySQL result set, the next thing to do is define the structure of a sample template file, named “default_template.htm,” that will be parsed in turn by the “TemplateProcessor” class. Please look at the signature of this example template file:

As you can see, the sample template file shown above has some placeholders located in different sections of the web page, which will be removed and replaced with actual data. In this example, both {header} and {footer} placeholders will be replaced with the dynamic output of the “header.php” and “footer.php” files respectively, while {maincontent} will be filled with the MySQL data set that I mentioned before. In addition, the nested {navbar{subnavigationbar1}{subnavigationbar2}} placeholders will be replaced with the contents of the following navigational PHP files:

Right, I think that all the above dynamic PHP files, in addition to the MySQL dataset that will be fetched in turn, are enough input data for feeding the input of the “TemplateProcessor” class and seeing how it works. Therefore, go ahead read the next section to see how the corresponding template file is parsed.

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:

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!