Perl Form Builder ::Features ::Download ::Tutorial ::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute
Note: The mailing list is has been replaced by the Google FormBuilder Group

Basic

1. Intro to FormBuilder
2. A Simple Form
3. Form Method
4. Input Validation
5. Field Options
6. Multi-Select Lists
7. Field Data
8. Field Attributes
9. HTML Formatting
10. Custom Headers
S. Summary

Intermediate

1. Trickier Forms
2. Perl References
3. Default Values
4. CGI Stickiness
5. JavaScript Actions
6. Input Validation 2
7. Multiple Submits
8. JavaScript Actions 2
9. Modules & mod_perl
10. Smartness
S. Summary

Advanced

1. Remaining Features
2. Intro to Templates
3. HTML::Template
4. HTML::Template 2
5. Template Toolkit
6. Exploiting render()
7. Multi-Screen Apps
8. Multi-Screen Apps 2
9. Multi-Screen Apps 3
10. Custom Messages
S. Summary

Advanced Tutorial - Step 5: Template Toolkit Print

<< HTML::Template 2 | Exploiting render() >>

Template Toolkit is more flexible, but looks a little more like code in your HTML. Form fields are actually represented as properties of a form object, separated by dots. Each Template Toolkit action is denoted within square brackets and percent signs.

Here are the equivalent tags between template modules:

HTML::TemplateTemplate Toolkit
<tmpl_var js-head> [% form.jshead %]
<tmpl_var form-start> [% form.start %]
<tmpl_var form-reset> [% form.reset %]
<tmpl_var form-submit> [% form.submit %]
<tmpl_var form-end> [% form.end %]
<tmpl_var field-[name]> [% form.field.[name].field %]
<tmpl_var value-[name]> [% form.field.[name].values %]
<tmpl_var label-[name]> [% form.field.[name].label %]
<tmpl_var comment-[name]> [% form.field.[name].comment %]
<tmpl_loop loop-[name]> [% form.field.[name].options %]

So, a similar template to our original HTML::Template written in Template Toolkit may look like this:

<!-- email_form.tt2 -->
<head>
<title>foo.com</title>
[% form.jshead %] <!-- JavaScript validation code -->
<head>
<body bgcolor=black text=white>
<font face="times,serif">
<p>
Hi! Welcome to foo.com! Please tell us some basic info:
<p>
[% form.start %]
Your Name: [% form.field.name.field %]<br>
Your Email: [% form.field.email.field %]<br>
<p>
Join Mailing List? [% form.field.mlist.field %]
<p>
When you're done, hit the "Submit" button below:<br>
[% form.reset %] [% form.submit %] <!-- buttons -->
[% form.end %]
Since Template Toolkit can handle complex data structures, you can do fancy stuff you can't do in HTML::Template, like loop through fields:
[% FOREACH field = form.fields %]
    <tr valign="top">
      <td>
        [% field.required 
              ? "<b>$field.label</b>" 
              : field.label 
        %]
      <td>
      <td>
        [% IF field.invalid %]
        Missing or invalid entry, please try again.
    <br>
[% END %]
Again, just like with HTML::Template, your FormBuilder code does not change! With the exception of adding the template option, everything else about FormBuilder remains the exact same:
$form = CGI::FormBuilder->new(
             method => 'post',
             fields => \@fields,
             template => {
                 type => 'TT2',     # Template Toolkit
                 template => '/path/to/file.tmpl',
                 variable => 'form'
             }
        );
For more information on Template Toolkit, please consult its documentation.

<< HTML::Template 2 | Exploiting render() >>

FormBuilder is © Nate Wiger, with contributions from many people.
Nateware