HomePHP 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.
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.
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!