|
::Features
::Download
::Tutorial
::Examples
::Documentation ::Join the Mailing List ::List Archives ::Contribute |
|
NAMECGI::FormBuilder::Template::HTML - FormBuilder interface to HTML::Template
SYNOPSIS
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => 'form.tmpl',
);
DESCRIPTIONThis engine adapts FormBuilder to use
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => 'form.tmpl',
);
Or, you can specify any options which
my $form = CGI::FormBuilder->new(
fields => \@fields,
template => {
type => 'HTML',
filename => 'form.tmpl',
shared_cache => 1,
loop_context_vars => 1
}
);
In your template, each of the form fields will correspond directly to
a In addition, there are a couple special fields:
<tmpl_var js-head> - JavaScript to stick in <head>
<tmpl_var form-title> - The <title> of the HTML form
<tmpl_var form-start> - Opening <form> tag and internal fields
<tmpl_var form-submit> - The submit button(s)
<tmpl_var form-reset> - The reset button
<tmpl_var form-end> - Just the closing </form> tag
Let's look at an example
<html>
<head>
<title>User Information</title>
<tmpl_var js-head><!-- this holds the JavaScript code -->
</head>
<tmpl_var form-start><!-- this holds the initial form tag -->
<h3>User Information</h3>
Please fill out the following information:
<!-- each of these tmpl_var's corresponds to a field -->
<p>Your full name: <tmpl_var field-name>
<p>Your email address: <tmpl_var field-email>
<p>Choose a password: <tmpl_var field-password>
<p>Please confirm it: <tmpl_var field-confirm_password>
<p>Your home zipcode: <tmpl_var field-zipcode>
<p>
<tmpl_var form-submit><!-- this holds the form submit button -->
</form><!-- can also use "tmpl_var form-end", same thing -->
As you see, you get a However, you may want even more control. That is, maybe you want
to specify every nitty-gritty detail of your input fields, and
just want this module to take care of the statefulness of the
values. This is no problem, since this module also provides
several other
<tmpl_var value-[field]> - The value of a given field
<tmpl_var label-[field]> - The human-readable label
<tmpl_var comment-[field]> - Any optional comment
<tmpl_var error-[field]> - Error text if validation fails
This means you could say something like this in your template:
<tmpl_var label-email>:
<input type="text" name="email" value="<tmpl_var value-email>">
<font size="-1"><i><tmpl_var error-email></i></font>
And FormBuilder would take care of the value stickiness for you,
while you have control over the specifics of the
Email:
<input type="text" name="email" value="nate@wiger.org">
<font size="-1"><i>You must enter a valid value</i></font>
Note, though, that this will only get the first value in the case
of a multi-value parameter (for example, a multi-select list). To
remedy this, if there are multiple values you will also get a
myapp.cgi?color=gray&color=red&color=blue
This would give the
<select name="color" multiple>
<tmpl_loop loop-color>
<option value="<tmpl_var value>"><tmpl_var label></option>
</tmpl_loop>
</select>
With
Inside <tmpl_loop>, this... Gives you this
--------------------------- -------------------------------
<tmpl_var value> value of that option
<tmpl_var label> label for that option
<tmpl_var checked> if selected, the word "checked"
<tmpl_var selected> if selected, the word "selected"
Please note that When you need precise control in a template this is all exposed to you;
normally FormBuilder does all this magic for you. If you don't need
exact control over your lists, simply use the But, let's assume you need exact control over your lists. Here's an example select list template:
<select name="color" multiple>
<tmpl_loop loop-color>
<option value="<tmpl_var value>" <tmpl_var selected>><tmpl_var label>
</tmpl_loop>
</select>
Then, your Perl code would fiddle the field as follows:
$form->field(
name => 'color', nameopts => 1,
options => [qw(red green blue yellow black white gray)]
);
Assuming query string as shown above, the template would then be expanded to something like this:
<select name="color" multiple>
<option value="red" selected>Red
<option value="green" >Green
<option value="blue" selected>Blue
<option value="yellow" >Yellow
<option value="black" >Black
<option value="white" >White
<option value="gray" selected>Gray
</select>
Notice that the Let's look at one last example. Here we want a radio group that allows a person to remove themself from a mailing list. Here's our template:
Do you want to be on our mailing list?
<p><table>
<tmpl_loop loop-mailopt>
<td bgcolor="silver">
<input type="radio" name="mailopt" value="<tmpl_var value>">
</td>
<td bgcolor="white"><tmpl_var label></td>
</tmpl_loop>
</table>
Then, we would twiddle our
$form->field(
name => 'mailopt',
options => [
[ 1 => 'Yes, please keep me on it!' ],
[ 0 => 'No, remove me immediately.' ]
]
);
When the template is rendered, the result would be something like this:
Do you want to be on our mailing list?
<p><table>
<td bgcolor="silver">
<input type="radio" name="mailopt" value="1">
</td>
<td bgcolor="white">Yes, please keep me on it!</td>
<td bgcolor="silver">
<input type="radio" name="mailopt" value="0">
</td>
<td bgcolor="white">No, remove me immediately</td>
</table>
When the form was then sumbmitted, you would access the values just like any other field:
if ($form->field('mailopt')) {
# is 1, so add them
} else {
# is 0, remove them
}
Finally, you can also loop through each of the fields using the top-level
<table>
<tmpl_loop fields>
<tr>
<td class="small"><tmpl_var label></td>
<td><tmpl_var field></td>
</tr>
</tmpl_loop>
</table>
Each loop will have a For more information on templates, see the HTML::Template manpage.
SEE ALSOthe CGI::FormBuilder manpage, the CGI::FormBuilder::Template manpage, the HTML::Template manpage
REVISION$Id: HTML.pm,v 1.32 2006/02/24 01:42:29 nwiger Exp $
AUTHORCopyright (c) 2000-2006 Nate Wiger <nate@wiger.org>. All Rights Reserved. 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. |