PHP
  Home arrow PHP arrow Page 2 - Developing an Extensible Template Proc...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing an Extensible Template Processor in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-05-02

    Table of Contents:
  • Developing an Extensible Template Processor in PHP 5
  • Getting started: defining the basic structure of the template processor
  • Assembling the template system: coding the "TemplateProcessor" class
  • Coding the workhorse of the class: defining the "processTemplate()" method
  • Getting the "TemplateProcessor" class completed: defining the remaining class methods

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing an Extensible Template Processor in PHP 5 - Getting started: defining the basic structure of the template processor


    (Page 2 of 5 )

    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">
    <html>
    <head>
    <title>{title}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <link rel="stylesheet" type="text/css" href="mystyles.css" />
    </head>
    <body>
    <div id="header">{header}</div>
    <div id="navbar">{navbar {subnavigationbar1}{ subnavigationbar2}}
    </div>
    <div id="leftcol">{leftcontent}</div>
    <div id="content">{maincontent}</div>
    <div id="rightcol">{rightcontent}</div>
    <div id="footer">{footer}</div>
    </body>
    </html>

    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


       · In this first article, you'll learn how to build the structure of the template...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT