HomePHP Page 3 - Template-Based Web Development With patTemplate (part 1)
Message In A Bottle - 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.
The block above represents a single template, identified by
the opening and closing <patTemplate:tmpl> tags, and by a unique name ("message", in this case).
Within the opening and closing tags comes the actual template body; to all intents and purposes, this is a regular HTML document, except that the markup also contains some special patTemplate variables, written in uppercase and enclosed within curly braces. These special variables will be replaced with actual values once the template engine gets its mitts on it. Let's look at that next.{mospagebreak title=Anatomy Of A Template Engine} Next, it's time to initialize the template engine and have it populate the template created on the previous page with actual data. Here's how:
<?php
// include the class
include("patTemplate.php");
// initialize an object of the class
$template = new patTemplate();
// set template location
$template->setBasedir("templates");
// set name of template file
$template->readTemplatesFromFile("message.tmpl");
// set values for template variables
$template->addVar("message", "TITLE", "Error");
$template->addVar("message", "MESSAGE", "Total system meltdown in
progress");
// parse and display the template
$template->DisplayParsedTemplate("message");
?>
This might seem a little complicated, so let me dissect it
for you:
1. The first step is, obviously, to include all the relevant files for the template engine. Since patTemplate is packaged as a single class, all I need to do is include that class file.
// include the class
include("patTemplate.php");
Once that's done, I can safely create an object of the
patTemplate class.
// initialize an object of the class
$template = new patTemplate();
This object instance will serve as the primary control point
for the template engine, allowing me to do all kinds of nifty things.
2. Next, the object's setBaseDir() and readTemplatesFromFile() methods are used to point the engine in the direction of the templates to be read. The setBaseDir() method sets the default location for all template files; the readTemplatesFromFile() method specifies which template file to process.
// set template location
$template->setBasedir("templates");
// set name of template file
$template->readTemplatesFromFile("message.tmpl");
patTemplate allows multiple templates to be stored in the
same physical file; the engine uses each template's name to uniquely identify it.
3. Once all the templates in the specified file have been read, it's time to do a little variable interpolation. This is accomplished via the addVar() object method, which attaches values to template variables.
// set values for template variables
$template->addVar("message", "TITLE", "Error");
$template->addVar("message", "MESSAGE", "Total system meltdown in
progress");
In English, this translates to "find the template named
'message' and assign the value 'Error' to the placeholder {TITLE} within it ".
You can run addVar() as many times as you like to perform variable interpolation.
4. Once you're done with all the variable replacement, all that's left is to display the final product.
// parse and display the template
$template->DisplayParsedTemplate("message");
The DisplayParsedTemplate() object method parses the
specified template, replacing all variables within it with their specified values, and outputs it to the output device.
In this specific example, the call to DisplayParsedTemplate() could also be broken down into two separate components, which combine to produce an equivalent result.
// parse and display the template
$template->parseTemplate("message");
echo $template->getParsedTemplate("message");
In this version, the call to parseTemplate() merely parses
the named template and replaces variables within it with appropriate values, while the call to getParsedTemplate() gets the contents of the parsed template and places it in a string variable.
As you will see, this alternative version has its uses, especially when it comes to iteratively building or repeating a Web page. This is discussed in detail a little further along - for the moment, just feast your eyes on the result of all the work above:
The nice thing about this approach? The page interface and page elements are separated from the program code that actually makes the page function - and can therefore be updated independently by Web designers and PHP developers.