Web Development With PHP FastTemplate - Musical Chairs
(Page 8 of 9 )
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:

Here are the templates I plan to use:
<!-- begin: main.tpl -->
<html>
<head>
<basefont face="Verdana">
</head>
<body link="Navy" vlink="Navy" alink="Navy">
<!-- standard header -->
<table width="100%">
<tr>
<td bgcolor=navy height=50> </td>
</tr>
</table>
<!-- table for quick links -->
<table width="100%" border="0" cellspacing="10" cellpadding="10">
<tr>
{LINKS}
</tr>
</table>
<p>
<!-- main story -->
{ARTICLE}
<!-- standard footer -->
<hr>
<center><font size=-2>All content copyright and proprietary Melonfire,
2001. All rights reserved.</font></center>
</body>
</html>
<!-- end: main.tpl -->
<!-- begin: list.tpl -->
<!-- this generates the quick link boxes -->
<td valign=top>
<table border=1 cellspacing=0 cellpadding=5>
<tr><td align=center bgcolor="black">
<font color=white><b>{SECTION_TITLE}</b></font>
</td></tr>
<tr><td>
<ul>
{LIST}
</ul>
</td></tr>
</table>
</td>
<!-- end: list.tpl -->
<!-- begin: listitem.tpl -->
<!-- individual list items -->
<li><a href="story.php?id={ID}"><font size=-1>{ITEM}</font></a>
<!-- end: listitem.tpl -->
<!-- begin: article.tpl -->
<!-- story area -->
<table border="0" cellspacing="0" cellpadding="0">
<tr><td valign=top>
<img src="{IMAGE}" height=100 width=100 align="left" border=0 hspace=5
alt="">
<h3>{SLUG}</h3>
<p>
{STORY}
</td></tr>
</table>
<!-- end: article.tpl -->
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.
Here's what the output looks like:
Next: A Rose By Any Other Name... >>
More PHP Articles
More By icarus, (c) Melonfire