Web Development With PHP FastTemplate - Repeat Customers
(Page 5 of 9 )
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.
<!-- begin: message.tpl -->
<div align=center style="font-family: Verdana; font-size: 10pt">
{MESSAGE}
</div>
<p>
<!-- end: message.tpl -->
<!-- begin: welcome.tpl -->
<html>
<head>
</head>
<body>
{CONTENT}
<hr>
</body>
</html>
<!-- end: welcome.tpl -->
Here's the script which puts them together:
<?
// 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.
Next: Flavour Of The Month >>
More PHP Articles
More By icarus, (c) Melonfire