PHP
  Home arrow PHP arrow Page 4 - Working with Multiple Template Files to Separate Logic from Presentation
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

Working with Multiple Template Files to Separate Logic from Presentation
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek