PHP
  Home arrow PHP arrow Page 12 - Template-Based Web Development With patTemplate (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT