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