PHP
  Home arrow PHP arrow Page 4 - Using a Template Processor Class in PH...
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 
Mobile Linux 
App Generation ROI 
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

Using a Template Processor Class in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-05-09

    Table of Contents:
  • Using a Template Processor Class in PHP 5
  • Getting started using the “TemplateProcessor” class: a quick look at its definition
  • Parsing template files: defining the input tags for the “TemplateProcessor” class
  • Going one step further: seeing the “TemplateProcessor” class in action

  • 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


    Using a Template Processor Class in PHP 5 - Going one step further: seeing the “TemplateProcessor” class in action


    (Page 4 of 4 )

    Having previously defined some of the most representative data sources, such as dynamic PHP files and MySQL result sets, all of them included within the corresponding array of input tags, the final step to demonstrate how the “TemplateProcessor” class can be used consists of putting together all the pieces in a single PHP script and showing the pertinent parsed (X)HTML output. Below is a snippet of code that shows the template processor in action:

    try{
        // include 'MySQL' class files
        require_once 'mysqlclass.php';
        require_once 'resultclass.php';
        // connect to MySQL
        $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>'password',
    'database'=>'database'));
        // run SQL query
        $result=$db->query('SELECT * FROM users');
        // get query resource
        $queryResult=$result->getQueryResource();
        // define input tags for template processor class
        $tags=array('title'=>'PHP 5 Template Processor','header'=>'header.php','navbar'=>array
    ('subnavigationbar1'=>'subnavbar1.php','navigationbar2'=>
    'subnavbar2.php'),
    'leftcontent'=>'leftcontent','maincontent'=>$queryResult,
    'rightcontent'=>
    'rightcontent','footer'=>'footer.php');
        // instantiate a new template processor object
        $tpl=new TemplateProcessor($tags);
        // display compressed page
        echo $tpl->getHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As you can see, the above snippet demonstrates how to include all the input tags that I defined previously in one array, which is passed as argument to a template processor object. After parsing the example “default_template.htm” template file and displaying the processed web page, the output that I get on my browser is similar to this:

    In the above screenshot, you can see how the corresponding placeholders have been replaced with real data coming from the previous sample PHP files, as well as from the MySQL dataset. In this case, I displayed only a couple of rows from a “users” database table, but I’m sure you get the idea of how result sets are processed by the “TemplateProcessor” class.

    Also, with regard to the above image, the last thing worth noting is the recursive replacement of the “{navbar{subnavigationbar1}{subnavigationbar2}}" placeholders with the respective “subnavbar1.php” and “subnavbar2.php” PHP files. Even when the source code of this class is easy to grasp, you can see that its parsing features are really useful.

    Now that I have demonstrated how the “TemplateProcessor” class is used with a variety of mixed data sources, feel free to tweak it and modify it, in order to meet your specific requirements. The experience is fun and instructive.

    Bottom line

    In this article, you hopefully learned how to use the “TemplateProcessor” class, using a mixture of data sources, such as simple strings, dynamic PHP files and MySQL datasets. Also, you saw its recursive replacement capabilities in action, which can be very handy when working with complex template files.

    Nevertheless, this series isn’t finished yet. In the last tutorial, I’ll show you how the base structure of the template processor can be used to develop a production-level class, which not only exposes the features that you saw before, but also implements the required logic for working with chunked caching and multiple template files. You won’t wan to miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this tutorial, the template processor I developed during the previous article is...
       · Hi Alejandro,I have read many of your articles and have found all of them an...
       · Hi there,Thank you for your comments on my PHP article, as well as your kind...
       · Alejandro, That one doesn't work either?? I know practically nothing about regular...
       · Hello again,Sorry to hear about the syntax error again, but I used the class...
       · I got the same syntax error above - and like many find regular expressions rather...
       · 2 with reference to the above error - the problem is not in the regular expression...
       · First off, I'd like to thank you for commenting on my PHP article. And lastly, with...
       · just another note, the compressed html regex needs to escape for the line breaks...
       · Thank you for posting some improvements on the template parser class, since they're...
       · i tried the suggestion but it did not work so i edited it to (\r\n|\n) and it...
       · Thanks alejandro for the wonderful code, it took me a whole night of anylizing where...
       · Thank you for commenting on my PHP article. I see you modified the source code of...
       · Thank you for commenting on my PHP article. The correct regexp is the one you used,...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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