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