Home arrow PHP arrow Page 12 - Template-Based Web Development With patTemplate (part 2)

A Well-Formed Example - 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.

TABLE OF CONTENTS:
  1. Template-Based Web Development With patTemplate (part 2)
  2. Scoping It Down
  3. Speaking In Tongues
  4. Looping The Loop
  5. Legal Eagles
  6. Hide And Seek
  7. Setting Things Right
  8. Fortune Favours The Brave
  9. Running On Empty
  10. Simple Simon
  11. Brain Dump
  12. A Well-Formed Example
  13. Crash Bang Boom
  14. Endgame
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 10
June 19, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Finally, here's a composite example, this one using a conditional template to iteratively build a complete HTML form. The unique thing about this form: the form fields are completely configurable via a user-defined array, with multiple types of forms possible using the same template.

First, here are the templates I'll be using:


<!-- form.tmpl --> <!-- main page --> <patTemplate:tmpl name="form"> <html> <head><basefont face="Arial"></head> <body> <form action="processor.php" method="post"> <patTemplate:link src="fields" /> <input type="submit" value="Save"> </form> </body> </html> </patTemplate:tmpl> <!-- field list --> <!-- conditional template containing sub-templates for each field type --> <patTemplate:tmpl name="fields" type="condition" conditionvar="FIELD_TYPE"> <patTemplate:sub condition="text"> {LABEL} <br> <input type="text" name="{NAME}"> <p> </patTemplate:sub> <patTemplate:sub condition="textarea"> {LABEL} <br> <textarea name="{NAME}"></textarea> <p> </patTemplate:sub> <patTemplate:sub condition="password"> {LABEL} <br> <input type="password" name="{NAME}"> <p> </patTemplate:sub> </patTemplate:tmpl>
I've got two templates here: one for the main page and the outer form elements, and one conditional template which uses the {FIELD_TYPE} decision variable to render the form controls. Currently, my template only knows how to handle text fields, password fields and text areas - feel free to add more constructs here.

Now, I'm going to define a PHP array which will contain information on the fields I'd like to display in my form, together with their labels and names. This array is completely user-configurable, and may be defined at run time, from a database, configuration file or XML data source. Here's what it looks like:

// field list // in form field-name => array(field-type, field-label) $fields = array( 'fname' => array('text', 'First name'), 'lname' => array('text', 'Last name'), 'address' => array('textarea', 'Address'), 'tel' => array('text', 'Telephone number'), 'email' => array('text', 'Email address') );
Now, all I need is a script to process this array and use it to assign appropriate values to the templates above.

<?php // field list // in form field-name => array(field-type, field-label) $fields = array( 'fname' => array('text', 'First name'), 'lname' => array('text', 'Last name'), 'address' => array('textarea', 'Address'), 'tel' => array('text', 'Telephone number'), 'email' => array('text', 'Email address') ); // 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("form.tmpl"); // get field names as array $keys = array_keys($fields); // iterate throough array foreach ($keys as $k) { // set field type // iteratively build list of form fields $template->addVar("fields", "FIELD_TYPE", $fields[$k][0]); $template->addVar("fields", "NAME", $k); $template->addVar("fields", "LABEL", $fields[$k][1]); $template->parseTemplate("fields", "a"); } // parse and display the template $template->displayParsedTemplate("form"); ?>
In this script, I'm iterating through the array, extracting information on each field name and type, and using that information to parse and render the conditional "fields" template. The "a" parameter to parseTemplate() ensures that the contents of each run are appended to the previous run, thereby iteratively constructing a series of form fields. Finally, the complete set of fields is plugged into the main page and displayed via displayParseTemplate().

Here's what it looks like:



Want a different form? Simply alter the $fields array, and watch in awe as a new form is dynamically generated before your very eyes. You gotta admit, that's pretty cool!

 
 
>>> More PHP Articles          >>> More By Team Melonfire, (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 6 - Follow our Sitemap

Dev Shed Tutorial Topics: