PHP
  Home arrow PHP arrow Page 4 - Working with Multiple Template Files t...
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Working with Multiple Template Files to Separate Logic from Presentation
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-05-16

    Table of Contents:
  • Working with Multiple Template Files to Separate Logic from Presentation
  • Setting up the basics of chunked caching: defining multiple template files
  • Parsing multiple template files: redefining the “TemplateProcessor” class
  • Putting the “TemplateProcessor” class to work: setting up a concrete example

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with Multiple Template Files to Separate Logic from Presentation - Putting the “TemplateProcessor” class to work: setting up a concrete example


    (Page 4 of 4 )

    Undoubtedly, the best way to see the real capabilities of the “TemplateProcessor” class is simply by setting up an example that shows how all the template files I showed you previously are used together.

    In this example, I’ll specify different expiration times for each template file, where the “header_template.htm” file will have an expiry of 3600 seconds, then the “body_template.htm” file will be updated each 60 seconds, and finally the “footer_template.htm” file will be parsed at intervals of 3600 seconds too. Additionally, I’ll include a MySQL dataset within the main section of the web page, which will be fetched by the MySQL wrapping classes that I showed in the previous article (they won’t be listed here).

    Having specified the set of parameters for this example, here’s the code sample that uses the “TemplateProcessor” class:

    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();
        // build array of page sections and assign expiration values
    for each cache file (chunked caching)
        $pageSections=array('header'=>7200,'body'=>60,'footer'=>7200);
        // build template tags
        $tags=array('header'=>array('title'=>'Template
    Processor','header'=>'header.php'),'body'=>array
    ('maincontent'=>array('staticdata'=>'Static
    Data','dynamicdata'=>$queryResult)),'footer'=>array
    ('footer'=>'footer.php'));
        // instantiate template processor object
        $tpl=new TemplateProcessor($tags,$pageSections);
        // display parsed page
        echo $tpl->getHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As you can see, the above script first connects to MySQL and fetches a trivial result set from a “users” database table. Next, the corresponding “$pageSections” input array is defined, which contains the shortened names of each web page section along with their cache expiration times. At this stage, do you see how this set of parameters is used internally by the “TemplateProcessor” class? I hope you do.

    Now, turn your attention to the second $tags array; notice that this is a recursive array, where all the tags of a specific page section are the elements of another array too. Of course, you can go as deep as you want in defining recursive arrays, as long as the template files contain the appropriate structure of nested placeholders.

    Right, that was the hard part. Once the proper input arrays have been defined, they’re passed as parameters to an instance of the template processor class, and finally the whole web page is displayed by using the “getHTML()” method.

    Of course, you’ll have a much better idea of the functionality of the “TemplateProcessor” class if you test it for yourself, thus I included a ZIP file here (which is also linked to at the beginning of this article), containing the class and additional sample files. I hope you enjoy playing with the source code I provided you, and as usual, feel free to introduce your own modifications.

    Final thoughts

    In this last tutorial of the series I went through the making of a PHP 5 extensible template processor class, which not only has most of the features that you saw in my previous article, but also is capable of working with multiple template files, in this way implementing the so-called chunked caching. As you’ve seen here, the source code of the class is pretty easy to follow, which means that you shouldn’t have trouble tweaking it, in order to meet your personal requirements. See you in the next PHP tutorial!


    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.

       · Over the course of this article, you'll learn how to build a powerful template...
       · Hi, first of all great article.I am having trouble with this line:// clean up...
       · Hey, thank you for the kind comments on this article. Regarding your question, try...
       · USE SMARTY (template engine) and you will bew happy!LOOK...
       · Thank you for your feedback regarding this article. I agree with you that Smarty is...
       · Parse error: parse error, unexpected '{' in...
       · Thank you for your comments on this article. Regarding your question, I used the...
       · My server is running php 4 and PHP 5, i had to name the file...
       · I'm glad to know you fixed out the problem.Thank you again for the kind words on...
       · Awesome tutorial ... I've found this series not only a great primer for learning how...
       · Thank you for your kind words on my PHP article. I really appreciate your comments....
       · First of all thanks for you usefull template engine.Second: I'm not a skilled PHP...
       · Thank you for commenting on my PHP article. Now, you can have a readable version of...
       · Thank you very much for your prompt answer.I also found it by myself 5 minutes...
       · You're welcome Massi. I'm glad to know the problem was solved...
       · Hello Alejandro,thanks for the good tutorial, I have this code on...
       · Hi Roy,Thank you again for the comments on my PHP article. Concerning your...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway