HomePHP Page 2 - Developing an Extensible Template Processor in PHP 5
Getting started: defining the basic structure of the template processor - PHP
This is the first part of a three-part series that covers separating logic from presentation in PHP applications. In this article you will learn to develop a template system that is advanced enough to meet the requirements of a majority of applications.
A good point at which to start developing my template processor is with defining its basic structure. Essentially, all the functionality of the template system will be encapsulated within a single PHP 5 class, which will expose originally a few handy methods that can be easily expanded later on. Based on these prerequisites, here's how the structure of my "TemplateProcessor" class looks:
class TemplateProcessor { // data members declaration goes here public function __construct(){ } // determine if cache file is valid or not private function isCacheValid(){ } // process template file private function processTemplate(){ } // process input file private function processFile(){ } // write compressed data to cache file private function writeCache(){ } // read compressed data from cache file private function readCache(){ } // return overall output public function getHTML(){ } // return compressed output private function getCompressedHTML(){ } // send gzip encoding http header public function sendEncodingHeader(){ } }
As shown above, the "TemplateProcessor" class presents some structural methods that suggest in some way the tasks they'll perform further. At first glance, you can see I defined a set of three methods called "isCacheValid()," "readCache()" and "writeCache()" respectively. This means that the class will be capable of caching the (X)HTML output of the respective web page, after the template file used by the class has been parsed.
Now, turn your attention to the methods "getCompressedHTML()" and "sendEncodingHeader()." As their names indicate, the class will also be provided with the ability to transfer web page contents as encoded data, a handy feature that will help to reduce the download time of the parsed page. In addition, the "sendEncodingHeader()" method will send out to the client the appropriate HTTP header, in order to decompress the data and display parsed web page contents.
Finally, the "processTemplate()" and "processFile()" methods are the core engine of the class, since they'll perform in conjunction all the required processes for parsing the corresponding template file. As you'll see in a few moments, the class will be capable of parsing recursively a template file (that is, placeholders nested in other placeholders), along with iterating over MySQL result sets, parsing dynamic PHP files and executing PHP code.
Provided that all the class features I mentioned before are good enough for you, it's always possible to define even more methods, in order to extend the existing functionality of the "TemplateProcessor" class.
Fine, now you have a clear idea of how this class will work; however my above explanations would be rather useless if I don't first show you how the template file utilized by the class will look. Thus, here's a typical sample template file:
If you've worked before with template files, the one shown above should be pretty familiar to you. In this example, you can see how the "navbar" placeholder is comprised of two additional placeholders, called "subnavigationbar1" and "subnavigationbar2" respectively. In modern template software, recursive placeholder replacement is really a powerful feature that helps designers to expand the versatility of presentational layers, while making template files much more maintainable.
Now that you understand how the "TemplateProcessor" class will work, in addition to seeing the look and feel of a classic template file, it's time to move forward and start coding each of the class methods.
To learn more about this, please read the next section of the article.