Home arrow PHP arrow 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.

  1. Developing an Extensible Template Processor in PHP 5
  2. Getting started: defining the basic structure of the template processor
  3. Assembling the template system: coding the "TemplateProcessor" class
  4. Coding the workhorse of the class: defining the "processTemplate()" method
  5. Getting the "TemplateProcessor" class completed: defining the remaining class methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 02, 2006

print this article



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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<link rel="stylesheet" type="text/css" href="mystyles.css" />
<div id="header">{header}</div>
<div id="navbar">{navbar {subnavigationbar1}{ subnavigationbar2}}
<div id="leftcol">{leftcontent}</div>
<div id="content">{maincontent}</div>
<div id="rightcol">{rightcontent}</div>
<div id="footer">{footer}</div>

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: