PHP
  Home arrow PHP arrow Page 6 - Template-Based Web Development With patTemplate (part 1)
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? 
PHP

Template-Based Web Development With patTemplate (part 1)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2002-05-28


    Table of Contents:
  • Template-Based Web Development With patTemplate (part 1)
  • Hard Sell
  • Message In A Bottle
  • Slice And Dice
  • Music To Your Ears
  • Watching The Clock
  • A Bookworm In The Ointment
  • A Rose By Any Other Name...

  • 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


    Template-Based Web Development With patTemplate (part 1) - Watching The Clock
    ( Page 6 of 8 )

    Another interesting application of patTemplate involves using a single template to iteratively generate a sequence of markup elements; this comes in particularly handy when creating HTML constructs like lists and table rows.

    Here's a simple example, a template containing a single item:


    <!-- main page --> <patTemplate:tmpl name="body"> <html> <head> <basefont face="Arial"> </head> <body> <patTemplate:link src="sequence" /> </body> </html> </patTemplate:tmpl> <!-- item to be repeated --> <patTemplate:tmpl name="sequence"> It is now {TIME} o'clock. <br> </patTemplate:tmpl>
    Looks harmless, doesn't it? But patTemplate lets you turn that insipid-looking template into a full-fledged sequence, parsing it multiple times and adding the output generated at each pass to that generated in previous passes. Take a look:

    <?php // include the class include("include/patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // add templates to the template engine $template->readTemplatesFromFile("sequence.tmpl"); for ($x=1; $x<=12; $x++) { // assign values to template variables $template->AddVar("sequence", "TIME", $x); $template->parseTemplate("sequence", "a"); } // parse and display the template $template->displayParsedTemplate("body"); ?>
    Here's the output:



    In this case, every time parseTemplate() is invoked, the template variable {TIME} is replaced with a new value, and the resulting output is appended to the output generated in previous calls to parseTemplate(). This is made possible via the additional "a" - which stands for "append" - parameter in the call to parseTemplate().

    As you might imagine, this can come in particularly handy when you're building a Web page dynamically from a database, and need to repeat a similar sequence of markup elements a specific number of times. Here's another, more useful example:

    <!-- addressbook.tmpl --> <!-- main page --> <patTemplate:tmpl name="body"> <html> <head> <basefont face="Arial"> </head> <body> <h2>Address Book</h2> <table border="1" cellspacing="0" cellpadding="5"> <tr> <td align="center"><i>Name</i></td> <td align="center"><i>Address</i></td> <td align="center"><i>Tel</i></td> <td align="center"><i>Fax</i></td> </tr> <patTemplate:link src="row" /> </table> </body> </html> </patTemplate:tmpl> <!-- item to be repeated --> <patTemplate:tmpl name="row"> <tr> <td>{NAME}</td> <td>{ADDRESS}</td> <td>{TEL}</td> <td>{FAX}</td> </tr> </patTemplate:tmpl>
    Here's the PHP script that brings it together:

    <?php // include the class include("include/patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // add templates to the template engine $template->readTemplatesFromFile("addressbook.tmpl"); // open database connection $connection = mysql_connect("localhost", "someuser", "somepass") or die ("Unable to connect!"); // select database mysql_select_db("data") or die ("Unable to select database!"); // generate and execute query $query = "SELECT * FROM addressbook ORDER BY name"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { // iterate through resultset // assign values to template variables from resultset fields // iteratively build sequence of <tr>s while($row = mysql_fetch_object($result)) { $template->AddVar("row", "NAME", $row->name); $template->AddVar("row", "ADDRESS", $row->address); $template->AddVar("row", "TEL", $row->tel); $template->AddVar("row", "FAX", $row->fax); $template->parseTemplate("row", "a"); } } // close connection mysql_close($connection); // parse and display the template $template->displayParsedTemplate("body"); ?>
    This is similar to the previous example, except that, this time, I'm using a result set from a MySQL database query as the data source for the template. As this result set is processed, a table is iteratively constructed and rendered using basic units like table cells and rows.

    Here's what the end result looks like:



     
     
    >>> More PHP Articles          >>> More By Team Melonfire, (c) Melonfire
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT