HomePHP Page 5 - Web Development With PHP FastTemplate
Repeat Customers - PHP
Typically, most PHP-based Web sites use scripts which containintermingled PHP and HTML code. While this speeds up development, it alsohas a downside: an interface designer cannot modify page layouts or HTMLtemplates without the assistance of an experienced PHP developer. Well,there's a solution to the problem - and you'll be surprised to hear thatit's been around for quite a while. Say hello to PHP FastTemplate.
Another nifty little feature you'll find in FastTemplate is the ability to "nest" one template within another - in the following example, a welcome message template is nested within the main index page template.
<?
// index.php - welcome page// include class fileinclude("class.FastTemplate.php3");// instantiate new object$obj = new FastTemplate("./tmpl/");// assign names for template files$obj->define(array("welcome" => "welcome.tpl", "message" => "message.tpl"));// normally, this variable might be set from a cookie// uncomment this to see how the message changes// $repeat_visitor = 1; // assign values to FT variable within the template if ($repeat_visitor == 1) { $obj->assign("MESSAGE", "Welcome back! We've updated our catalog sinceyour last visit - click here to see the new arrivals."); } else { $obj->assign("MESSAGE", "You're visiting our site for the very first time,so you might like to take our New User Tour."); }// parse templates// in this case, "message" is parsed first// the resulting output is assigned to the FT variable CONTENT// the next template "welcome" is parsed$obj->parse(CONTENT, array("message", "welcome"));// and print$obj->FastPrint(CONTENT);?>
When the parse() method is assigned a series of templates to parse via an array, FastTemplate will proceed through the array in a sequential manner, assigning the result of each successive parse() operation to the variable specified.
In this case, FastTemplate will first parse the template "message", assign a value to the "MESSAGE" variable, and then assign the result to the variable "CONTENT". At this stage, the variable "CONTENT" contains:
<!-- begin: message.tpl -->
<div align=center style="font-family: Verdana; font-size: 10pt">
You're visiting our site for the very first time, so you might like to take
our New User Tour.
</div>
<p>
<!-- end: message.tpl -->
Next, it will proceed to parse "welcome", assign the value of the
newly-created variable "CONTENT" to the template, and again store the result in "CONTENT". At this stage, the variable "CONTENT" contains:
<!-- begin: welcome.tpl -->
<html>
<head>
</head>
<body>
<!-- begin: message.tpl -->
<div align=center style="font-family: Verdana; font-size: 10pt">
You're visiting our site for the very first time, so you might like to take
our New User Tour.
</div>
<p>
<!-- end: message.tpl -->
<hr>
</body>
</html>
<!-- end: welcome.tpl -->
This is what finally gets printed to the browser via
FastPrint().
Note also that you can assign values to FastTemplate variables on the basis of conditional tests; in the example above, the message changes depending on whether or not the user is a repeat visitor.