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

A Bookworm In The Ointment - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
I'd like to wrap up this introductory article with a comprehensive example, which builds on what you've learned so far to demonstrate how easy it is to use patTemplate to quickly construct different types of page layouts.

Let's suppose I wanted to generate a Web page for an item in an online book store, and let's further suppose that I wanted it to look like this:



Here are the templates I plan to use:

<!-- books.tmpl --> <!-- container page --> <patTemplate:tmpl name="main"> <html> <head> <basefont face="Arial"> </head> <body> <!-- header --> <img src="logo.gif" alt="Company logo"> <!-- content --> <table width="100%" cellspacing="5" cellpadding="5"> <tr> <td valign="top"> <patTemplate:link src="recommendations" /> </td> <td valign="top"> <patTemplate:link src="review" /> </td> </tr> </table> <!-- footer --> <hr> <center><font size=-2>All content copyright and proprietary <a href="http://www.melonfire.com/">Melonfire</a>, 2002. All rights reserved.</font></center> </body> </html> </patTemplate:tmpl> <!-- review section --> <patTemplate:tmpl name="review"> <h2>{TITLE}</h2> <p> <img src="{POSTER}" width="100" height="100" alt="Book jacket" align="left">{CONTENT} </patTemplate:tmpl> <!-- reco section --> <patTemplate:tmpl name="recommendations"> <font size="-1">If you liked this title, you might also like: <br> <ul> <patTemplate:link src="recommendations_list" /> </ul> </font> </patTemplate:tmpl> <!-- reco item section --> <patTemplate:tmpl name="recommendations_list"> <li><a href="story.php?id={ID}"><font size=-1>{ITEM}</font></a> <p> </patTemplate:tmpl>
I'm going to use these four templates to generate the layout illustrated above.

<?php /*** this entire section would come from a database ***/ // book details - title, content, poster $title = "On Ice"; $content = "A taut, well-written thriller, <i>On Ice</i> is a roller-coaster ride right from the opening paragraphs. Often surprising, never boring, David Ramus expertly guides his hero from one calamity to the next, deftly unveiling new parts of the puzzle until the pieces come together in the explosive showdown. While the pacing, especially in the novel's second half, may seem a little jagged, the first half of the novel, with its vivid characterization of prison life and snappy dialogue, is well worth a read.<p>Once more...<p>A taut, well-written thriller, <i>On Ice</i> is a roller-coaster ride right from the opening paragraphs. Often surprising, never boring, David Ramus expertly guides his hero from one calamity to the next, deftly unveiling new parts of the puzzle until the pieces come together in the explosive showdown. While the pacing, especially in the novel's second half, may seem a little jagged, the first half of the novel, with its vivid characterization of prison life and snappy dialogue, is well worth a read.<p>"; $image = "poster.gif"; // list of titles for recommendations $items = array(); $items[0] = "City Of Bones - Michael Connelly"; $items[1] = "Mortal Prey - John Sandford"; $items[2] = "Hostage - Robert Crais"; // corresponding review ids $ids = array(); $ids[0] = 23; $ids[1] = 124; $ids[2] = 65; /*** database action ends **/ // 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("books.tmpl"); $template->AddVar("review", "TITLE", $title); $template->AddVar("review", "POSTER", $image); $template->AddVar("review", "CONTENT", $content); // iterate through array // assign values to template variables from array fields // iteratively build list for ($x=0; $x<sizeof($items); $x++) { $template->AddVar("recommendations_list", "ID", $ids[$x]); $template->AddVar("recommendations_list", "ITEM", $items[$x]); $template->parseTemplate("recommendations_list", "a"); } // parse and display the template $template->displayParsedTemplate("main"); ?>
The first part of this script is focused solely on extracting information to display from a database - I've hard-coded the values here for demonstration purposes. Once the variables are set, the script initializes a patTemplate object and reads the four templates I plan to use into the template engine.

Next, values are assigned to the {TITLE}, {POSTER} and {CONTENT} variables within the "review" template. Once that's done, the list of recommendations is iteratively built, using the data in the $items and $ids arrays.

At the end of this process, the "recommendations_list" template stores a complete list of recommended books. This is then placed in the "recommendations" template, and, finally, both "recommendations" and "review" are transposed in the "main" template and printed to the browser.

Here's what the output looks like:



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: