HomePHP Page 9 - Template-Based Web Development With patTemplate (part 2)
Running On Empty - PHP
Got the basics down? Well, here's the advanced course - thisarticle demonstrates some of patTemplate's more sophisticated features,including the ability to dynamically show or hide templates, inheritvariables, use loops and conditional branches, and create dynamic,template-based forms and error handlers.
patTemplate also allows you to cover for the unexpected by specifying two additional sub-templates, one which is displayed when the decision variable cannot be matched, and one to be displayed when the decision variable is empty. Here's an example of how this might work:
<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>
<patTemplate:sub condition="default">
Sorry, closed on the weekend.
</patTemplate:sub>
<patTemplate:sub condition="empty">
Sorry, cannot determine day of week. Did the world just end?
</patTemplate:sub>
</body>
</html>
</patTemplate:tmpl>
In this case, the sub-template specified as "default" will
appear on Saturdays and Sundays, while the sub-template specified as "empty" will appear if {DAY} is empty. Try it out and see for yourself.
You can force patTemplate to look in the global namespace for the value of the decision variable in case it's not available locally, by adding the optional "useglobals" attribute to your template definition.