Home arrow PHP arrow Page 6 - Template-Based Web Development With patTemplate (part 1)

Watching The Clock - PHP

Most PHP-based Web sites are a mush of intermingled HTMLmarkupand PHP function calls, making them hard to decipher and maintain. Butthere *is* a simpler way - using templates to separate layout frombusinesslogic. This article shows you how.

  1. Template-Based Web Development With patTemplate (part 1)
  2. Hard Sell
  3. Message In A Bottle
  4. Slice And Dice
  5. Music To Your Ears
  6. Watching The Clock
  7. A Bookworm In The Ointment
  8. A Rose By Any Other Name...
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 9
May 28, 2002

print this article


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

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: