|
::Features
::Download
::Tutorial
::Examples
::Documentation ::Join the Mailing List ::List Archives ::Contribute |
|
NAMECGI::FormBuilder::Template::Text - FormBuilder interface to Text::Template
SYNOPSIS
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'Text',
template => 'form.tmpl',
variable => 'form',
}
);
DESCRIPTIONThis engine adapts FormBuilder to use
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'Text', # use Text::Template
template => 'form.tmpl',
}
);
The default options passed into
TYPE => 'FILE'
SOURCE => 'form.tmpl'
DELIMITERS => ['<%','%>']
As these params are passed for you, your template will look very similar to
ones used by Template Toolkit and
<% $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)
Note that you refer to variables with a preceding
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'Text',
template => 'form.tmpl',
variable => 'form'
},
);
Unlike Template Toolkit, though, these will not be placed in OO-style, dot-separated vars. Instead, a hash will be created which you then reference:
<% $form{jshead} %>
<% $form{start} %>
etc.
And field data is in a hash-of-hashrefs format:
For a field named... The field data is in...
-------------------- -----------------------
job <% $form{field}{job} %]
size <% $form{field}{size} %]
email <% $form{field}{email} %]
Since
<%
my $myfield = $form{field}{email};
$myfield->{label}; # text label
$myfield->{field}; # field input tag
$myfield->{value}; # first value
$myfield->{values}; # list of all values
$myfield->{options}; # list of all options
$myfield->{required}; # required flag
$myfield->{invalid}; # invalid flag
$myfield->{error}; # error string if invalid
%>
<%
for my $field (@{$form{fields}}) {
$OUT .= "<tr>\n<td>" . $field->{label} . "</td> <td>"
. $field->{field} . "</td>\n<tr>";
}
%>
In addition, when using the engine option, you supply an existing
Text::Template object or a hash of parameters to be passed to
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'Text',
template => 'form.tmpl',
variable => 'form',
engine => {
DELIMITERS => [ '[@--', '--@]' ],
},
data => {
version => 1.23,
author => 'Fred Smith',
},
},
);
If you pass a hash of parameters, you can override the
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'Text',
variable => 'form',
engine => {
TYPE => 'STRING',
SOURCE => $string,
DELIMITERS => [ '[@--', '--@]' ],
},
data => {
version => 1.23,
author => 'Fred Smith',
},
},
);
If you get the crazy idea to let users of your application pick the template file
(strongly discouraged) and you're getting errors, look at the Also, note that If you're really stuck on this, though, a workaround is to say:
PREPEND => 'use strict; use vars qw(%form);'
and then set the option Finally, when you use the
data => {
anArray => [ 1, 2, 3 ],
aHash => { orange => 'tangy', chocolate => 'sweet' },
}
This becomes the following in your template:
<%
@anArray; # you can use $myArray[1] etc.
%aHash; # you can use $myHash{chocolate} etc.
%>
For more information, please consult the
SEE ALSOthe CGI::FormBuilder manpage, the CGI::FormBuilder::Template manpage, the Text::Template manpage
REVISION$Id: Text.pm,v 1.17 2005/03/14 19:31:56 nwiger Exp $
AUTHORCopyright (c) 2000-2005 Nathan Wiger <nate@sun.com>. All Rights Reserved. Text::Template support is due to huge contributions by Jonathan Buhacoff. Thanks man. 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. |