Template-Based Web Development With patTemplate (part 2) - Fortune Favours The Brave (
Page 8 of 14 )
Thus far, I've been working with what
patTemplate refers to as "standard" templates. However, the patTemplate class
also comes with some decidedly non-standard - and rather cool -
alternatives...and one of the neatest ones has to be its support for conditional
templates.
Conditional templates function much like a series of "if-else"
conditional statements - they allow you to display different output depending on
how a particular, user-defined condition is evaluated. A conditional template
typically contains a number of sub-templates, each keyed against a particular
variable; depending on the value of that variable, the appropriate sub-template
is extracted and used.
In order to better understand this, consider the
following simple example:
<patTemplate:tmpl name="fortune" type="condition" conditionvar="DAY">
<html> <head> <basefont face="Arial"> </head>
<body>
And today's fortune is:
<br>
<patTemplate:sub condition="Mon">
Never make anything simple and efficient when a way can be found to make
it complex and wonderful. </patTemplate:sub>
<patTemplate:sub condition="Tue">
Life is a game of bridge -- and you've just been finessed.
</patTemplate:sub>
<patTemplate:sub condition="Wed">
What sane person could live in this world and not be crazy?
</patTemplate:sub>
<patTemplate:sub condition="Thu">
Don't get mad, get interest.
</patTemplate:sub>
<patTemplate:sub condition="Fri">
Just go with the flow control, roll with the crunches, and, when you get
a prompt, type like hell. </patTemplate:sub>
</body>
</html>
</patTemplate:tmpl>
A little analysis, and you'll see that this isn't as
complicated as it looks. The outer "fortune" template has been defined as a
conditional template by the addition of two attributes to the
<patTemplate:tmpl> tag - the "type" attribute, which is set to the value
"condition", and the "conditionvar" attribute, which is set to the name of the
decision variable to be used during the evaluation process.
This
conditional template is then broken up into individual sub-templates, enclosed
within <patTemplate:sub>...</patTemplate:sub> tags, and each
possessing a "condition" attribute. This condition attribute specifies the value
of the decision variable that the template engine will use when deciding which
sub-template to display.
Here's the other half of the puzzle - the PHP
script that actually sets a value for the decision variable so that the template
engine can select an appropriate sub-template.
<?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("fortune.tmpl");
$template->AddVar("fortune", "DAY", date("D", mktime()));
// parse and display the template
$template->displayParsedTemplate("fortune");
?>
In this case, the PHP script merely sets the value of the
template variable {DAY} - you'll remember that this is the decision variable
defined in the conditional template - to the current day of the week, and then
displays the parsed template. Internally, the template engine will match the
value of {DAY} to the options available in the various sub-templates, and pick
the one that fits.
As you can see, this is identical to a "switch"
statement, or a series of "if-else" conditional statements - and it can come in
fairly handy at times.