Home arrow PHP arrow Page 4 - Working with Multiple Template Files to Separate Logic from Presentation

Putting the “TemplateProcessor” class to work: setting up a concrete example - PHP

Welcome to the last part of the series “Separating logic from presentation.” In three tutorials, this series teaches you how to develop an expandable template processor class using PHP 5, which exposes some useful features, such as recursive placeholder replacement, MySQL result sets processing, and parsing of dynamic PHP files, among others.

  1. Working with Multiple Template Files to Separate Logic from Presentation
  2. Setting up the basics of chunked caching: defining multiple template files
  3. Parsing multiple template files: redefining the “TemplateProcessor” class
  4. Putting the “TemplateProcessor” class to work: setting up a concrete example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
May 16, 2006

print this article



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:

    // include 'MySQL' class files
    require_once 'mysqlclass.php';
    require_once 'resultclass.php';
    // connect to MySQL
    $db=new MySQL(array
    // run SQL query
    $result=$db->query('SELECT * FROM users');
    // get query resource
    // build array of page sections and assign expiration values
for each cache file (chunked caching)
    // build template tags
    // instantiate template processor object
    $tpl=new TemplateProcessor($tags,$pageSections);
    // display parsed page
    echo $tpl->getHTML();
catch(Exception $e){
    echo $e->getMessage();

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: