HomePHP Page 10 - Template-Based Web Development With patTemplate (part 2)
Simple Simon - 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.
In case the standard template type doesn't meet your needs, and the conditional one is too complicated, patTemplate offers you the best of both worlds with its "simpleCondition" template type. This template type defines a list of variables that are required for the template to be displayed; if these variables don't exist, the template will never be displayed.
With this in mind, it's possible to simplify and rewrite the example on the previous page as an illustration of the concept:
<patTemplate:tmpl name="fortune" type="simpleCondition"
requiredvars="DAY"> <html> <head> <basefont face="Arial"> </head>
<body>
And today's fortune is:
<br>
What sane person could live in this world and not be crazy?
</body>
</html>
</patTemplate:tmpl>
And here's the script:
<?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");
// comment the next line and the template will never be displayed
$template->AddVar("fortune", "DAY", date("D", mktime()));
// parse and display the template
$template->displayParsedTemplate("fortune");
?>