PHP
  Home arrow PHP arrow Page 2 - Developing an Extensible Template Processor in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Developing an Extensible Template Processor in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek