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