Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Web Development With PHP FastTemplate
  2. Who Am I?
  3. Proofing The Pudding
  4. You've Got Mail
  5. Repeat Customers
  6. Flavour Of The Month
  7. A Strict() Master
  8. Musical Chairs
  9. A Rose By Any Other Name...
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
September 05, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: