PHP
  Home arrow PHP arrow Page 6 - Template-Based Web Development With pa...
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 
Moblin 
JMSL Numerical Library 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      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


    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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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