HomePHP Page 8 - Web Development With PHP FastTemplate
Musical Chairs - PHP
Typically, most PHP-based Web sites use scripts which containintermingled PHP and HTML code. While this speeds up development, it alsohas a downside: an interface designer cannot modify page layouts or HTMLtemplates without the assistance of an experienced PHP developer. Well,there's a solution to the problem - and you'll be surprised to hear thatit's been around for quite a while. Say hello to PHP FastTemplate.
I'd like to wrap up this article with a comprehensive example, which demonstrates how easy it is to use FastTemplate to quickly build different types of page layouts.
Let's suppose I wanted to generate a Web page containing music news and reviews, and let's further suppose that it looks like this:
I'm going to use these four templates to generate the layout illustrated
above.
<?
/*** this entire section would come from a database ***/
// article details - title, content, poster
$slug = "Yellow Gold";
$story = "Coldplay's debut album, <i>Parachutes</i>, has won them a bunch
of awards, and the first single, <i>Yellow</i>, has been receiving a fair
amount of airplay in both Europe and America. But if you want to understand
why this band is regarded as one of best new acts to have emerged from
England, you need to dig a little deeper...<p> Let's repeat that once
again, shall we? <p> Coldplay's debut album, <i>Parachutes</i>, has won
them a bunch of awards, and the first single, <i>Yellow</i>, has been
receiving a fair amount of airplay in both Europe and America. But if you
want to understand why this band is regarded as one of best new acts to
have emerged from England, you need to dig a little deeper...<p>";
$image = "poster.gif";
// list of sections
$sections = array("Pop", "Rock", "Dance");
// list of titles for quick links
// set as a 2D array
$items = array();
// pop links
$items[0][0] = "All For You - Janet Jackson";
$items[0][1] = "Room Service - Roxette";
$items[0][2] = "No Strings Attached - N-Sync";
// rock links
$items[1][0] = "Jaded - Aerosmith";
$items[1][1] = "All That You Can't Leave Behind - U2";
$items[1][2] = "Parachutes - Coldplay";
$items[1][3] = "Everything You Want - Vertical Horizon";
// dance links
$items[2][0] = "Spin - Collection";
$items[2][1] = "Buddha Bar - Collection";
// corresponding story ids
$ids = array();
$ids[0][0] = 23;
$ids[0][1] = 124;
$ids[0][2] = 65;
$ids[1][0] = 63;
$ids[1][1] = 234;
$ids[1][2] = 43;
$ids[1][3] = 533;
$ids[2][0] = 12;
$ids[2][1] = 239;
/*** database action ends **/
// include class file
include("class.FastTemplate.php3");
// instantiate new object
$obj = new FastTemplate("./tmpl/");
// assign names for template files
$obj->define(array(
"main" => "main.tpl",
"list" => "list.tpl",
"listitem" => "listitem.tpl",
"article" => "article.tpl"
));
// assign variables
$obj->assign(array(
"SLUG" => $slug,
"STORY" => $story,
"IMAGE" => $image
));
// this section builds the list items, and then the different list boxes
for ($x=0; $x<sizeof($sections); $x++)
{
// first loop through section list
// get the name of this section
$obj->assign("SECTION_TITLE", $sections[$x]);
// this loop is to build the <li> items
for ($y=0; $y<sizeof($items[$x]); $y++)
{
$obj->assign(array(
"ITEM" => $items[$x][$y],
"ID" => $ids[$x][$y]
));
// each item is added to the previous
$obj->parse("LIST", ".listitem");
}
// at this stage, the list is complete
// the complete list is assigned (appended) to the end of a new variable
$obj->parse("LINKS", ".list");
// clear the LIST variable for the next series
$obj->clear("LIST");
}
// parse templates
$obj->parse(ARTICLE, "article");
$obj->parse(RESULT, "main");
// and print
$obj->FastPrint(RESULT);
?>
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 FastTemplate object and defines names for the four templates I plan to use.
Since the article template is the simplest, the script assigns values to the "STORY", "SLUG" and "IMAGE" variables first. Once that's done, the various link boxes are built up and appended to the "LINKS" variable using the "." operator.
At the end of this process, the "LINKS" variable stores all the HTML code required to generate the three boxes at the top of the page. Next, the "article.tpl" template is parsed and values assigned to its variables; the result is then stored in the "ARTICLE" variable. Finally, the values of both the "ARTICLE" and "LINKS" variables are transposed in the "main.tpl" template, and the result printed to the browser.