HomePHP Working with Multiple Template Files to Separate Logic from Presentation
Working with Multiple Template Files to Separate Logic from Presentation
Welcome to the last part of the series “Separating logic from presentation.” In three tutorials, this series teaches you how to develop an expandable template processor class using PHP 5, which exposes some useful features, such as recursive placeholder replacement, MySQL result sets processing, and parsing of dynamic PHP files, among others.
A downloadable file for this article is available here.
Introduction
As you’ll hopefully recall, in the previous article I set up an illustrative hands-on example, aimed at demonstrating the functionality of the “TemplateProcessor” class that I wrote at the beginning of the series. With reference to this example, I created some basic dynamic PHP files, aside from fetching a small MySQL result set, and finally integrated all these data sources within an array of input tags, in order to show how the template processor class was capable of parsing a given template file.
Although the sample PHP files that I included as elements of the respective array of input tags were rather basic, they allowed me to illustrate the way that the “TemplateProcessor” class replaces the placeholders in question with data coming from different data sources. As I said repeatedly over the previous tutorials, the structure of this class is very expandable, thus if you need even more template processing features, in accordance with your particular development requirements, you can easily add more methods to the class, or rewrite the existing ones.
Now, by returning to the subject of this last installment, I’ll use the skeleton of the original “TemplateProcessor” class to develop an improved, production-level template processor, which, as you’ll see in a few moments, will be capable of working with multiple template files, in addition to implementing a chunked caching system. In this way, the class will be able to use several templates that have distinct cache expiration times, aside from utilizing most of the template processing features that you learned before.
Are you ready to start learning how to code this improved template processor? Right, let’s do it together.