Home arrow PHP arrow Page 6 - Web Development With PHP FastTemplate

Flavour Of The Month - 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 useful FastTemplate feature involves creating a sequence of items by appending to a single variable; this comes in particularly handy when creating HTML constructs like lists and table rows. Consider the following templates:

<!-- begin: list.tpl --> <html> <head> </head> <body> <ul> {LISTCONTENT} </ul> </body> </html> <!-- end: list.tpl --> <!-- begin: list_item.tpl --> <li>{ITEM} <!-- end: list_item.tpl -->
In this case, I plan to build a list (from a database) by repeatedly calling the "list_item.tpl" template; this list will then be assigned to a FastTemplate variable named "LISTCONTENT", and substituted in the main page, "list.tpl". Here's the script:

<? // list.php - item list // include class file include("class.FastTemplate.php3"); // instantiate new object $obj = new FastTemplate("./tmpl/"); // assign names for template files $obj->define(array( "list" => "list.tpl", "list_item" => "list_item.tpl" )); // get item list /* // open connection to database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); // query $query = "SELECT item from itemtable"; $result = mysql_db_query($database, $query, $connection); // build $items array $items = array(); while(list($item) = mysql_fetch_row($result)) { $items[$count] = $item; $count++; } */ // assume it looks like this.. $items = array("vanilla", "pineapple", "strawberry", "chocolate chip", "peach", "banana", "grape"); // build LISTCONTENT variable by concatenation of multiple instances of "list_item" template for ($x=0; $x<sizeof($items); $x++) { $obj->assign("ITEM", $items[$x]); // append the result of parsing the template to LISTCONTENT $obj->parse(LISTCONTENT, ".list_item"); } // parse templates $obj->parse(RESULT, "list"); // and print $obj->FastPrint(RESULT); ?>
Each time the loop iterates through the $items array, a new value is assigned to the "ITEM" variable. FastTemplate then parses the "list_item" template and replaces the placeholder with its actual value. The "." operator is used to append the result of each iteration to the "LISTCONTENT" variable.

This technique comes in very handy when building repetitive sequences - all you need to do is build a template containing one item of the sequence, and let FastTemplate generate as many copies of it as you need.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: