|
::Features
::Download
::Tutorial
::Examples
::Documentation ::Join the Mailing List ::List Archives ::Contribute |
|
NAMECGI::FormBuilder::Template::TT2 - FormBuilder interface to Template Toolkit
SYNOPSIS
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'TT2',
template => 'form.tmpl',
variable => 'form',
}
);
DESCRIPTIONThis engine adapts FormBuilder to use
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'TT2',
template => 'userinfo.tmpl',
variable => 'form',
}
);
The following methods are provided (usually only used internally):
engineReturns a reference to the
prepareReturns a hash of all the fields ready to be rendered.
renderUses the prepared hash and expands the template, returning a string of HTML.
TEMPLATESThe template might look something like this:
<html>
<head>
<title>[% form.title %]</title>
[% form.jshead %]
</head>
<body>
[% form.start %]
<table>
[% 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 %]
[% field.field %]
</td>
</tr>
[% END %]
<tr>
<td colspan="2" align="center">
[% form.submit %] [% form.reset %]
</td>
</tr>
</table>
[% form.end %]
</body>
</html>
By default, the Template Toolkit makes all the form and field information accessible through simple variables.
[% jshead %] - JavaScript to stick in <head>
[% title %] - The <title> of the HTML form
[% start %] - Opening <form> tag and internal fields
[% submit %] - The submit button(s)
[% reset %] - The reset button
[% end %] - Closing </form> tag
[% fields %] - List of fields
[% field %] - Hash of fields (for lookup by name)
You can specify the
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'TT2',
template => 'form.tmpl',
variable => 'form'
},
);
With
[% form.jshead %]
[% form.start %]
etc.
You can access individual fields via the
For a field named... The field data is in...
-------------------- -----------------------
job [% form.field.job %]
size [% form.field.size %]
email [% form.field.email %]
Each field contains various elements. For example:
[% myfield = form.field.email %]
[% myfield.label %] # text label
[% myfield.field %] # field input tag
[% myfield.value %] # first value
[% myfield.values %] # list of all values
[% myfield.option %] # first value
[% myfield.options %] # list of all values
[% myfield.required %] # required flag
[% myfield.invalid %] # invalid flag
The
[% FOREACH field = form.fields %]
<tr>
<td>[% field.label %]</td> <td>[% field.field %]</td>
</tr>
[% END %]
If you want to customise any of the Template Toolkit options, you can
set the
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'TT2',
template => 'form.tmpl',
variable => 'form',
engine => {
INCLUDE_PATH => '/usr/local/tt2/templates',
},
data => {
version => 1.23,
author => 'Fred Smith',
},
},
);
For further details on using the Template Toolkit, see
SEE ALSOthe CGI::FormBuilder manpage, the CGI::FormBuilder::Template manpage, Template
REVISION$Id: TT2.pm 91 2006-12-18 10:27:01Z nwiger $
AUTHORCopyright (c) 2000-2006 Nate Wiger <nate@wiger.org>. All Rights Reserved. Template Tookit support is largely due to a huge patch from Andy Wardley. This module is free software; you may copy this under the terms of the GNU General Public License, or the Artistic License, copies of which should have accompanied your Perl kit. |
| FormBuilder is © 2000-2006 Nate Wiger, with contributions from many people. |