PHP
  Home arrow PHP arrow Page 12 - Template-Based Web Development With pa...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Template-Based Web Development With patTemplate (part 2)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2002-06-19

    Table of Contents:
  • Template-Based Web Development With patTemplate (part 2)
  • Scoping It Down
  • Speaking In Tongues
  • Looping The Loop
  • Legal Eagles
  • Hide And Seek
  • Setting Things Right
  • Fortune Favours The Brave
  • Running On Empty
  • Simple Simon
  • Brain Dump
  • A Well-Formed Example
  • Crash Bang Boom
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Template-Based Web Development With patTemplate (part 2) - A Well-Formed Example


    (Page 12 of 14 )

    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


     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway