CGI::FormBuilder::Template::HTML(3) User Contributed Perl Documentation NAME CGI::FormBuilder::Template::HTML - FormBuilder interface to HTML::Template SYNOPSIS my $form = CGI::FormBuilder->new( fields => \@fields, template => 'form.tmpl', ); DESCRIPTION This engine adapts FormBuilder to use "HTML::Template". "HTML::Template" is the default template option and is activated one of two ways. Either: my $form = CGI::FormBuilder->new( fields => \@fields, template => 'form.tmpl', ); Or, you can specify any options which "HTML::Template->new" accepts by using a hashref: my $form = CGI::FormBuilder->new( fields => \@fields, template => { type => 'HTML', filename => 'form.tmpl', shared_cache => 1, loop_context_vars => 1 } ); The following methods are provided (usually only used internally): engine Returns a reference to the "HTML::Template" object prepare Returns a hash of all the fields ready to be rendered. render Uses the prepared hash and expands the template, returning a string of HTML. TEMPLATES In your template, each of the form fields will correspond directly to a "" of the same name prefixed with "field-" in the template. So, if you defined a field called "email", then you would setup a variable called "" in your template. In addition, there are a couple special fields: - JavaScript to stick in - The 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 "form.tmpl" template we could use: <html> <head> <title>User Information

User Information

Please fill out the following information:

Your full name:

Your email address:

Choose a password:

Please confirm it:

Your home zipcode:

As you see, you get a "" for each for field you define. 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 "" tags as well: - The value of a given field - The human-readable label - Any optional comment - Error text if validation fails - See if the field is required This means you could say something like this in your template: : And FormBuilder would take care of the value stickiness for you, while you have control over the specifics of the "" tag. A sample expansion may create HTML like the following: Email: You must enter a valid value 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 "" prefixed with "loop-". So, if you had: myapp.cgi?color=gray&color=red&color=blue This would give the "color" field three values. To create a select list, you would do this in your template: With "" tags, each iteration gives you several variables: Inside , this... Gives you this --------------------------- ------------------------------- value of that option label for that option if selected, the word "checked" if selected, the word "selected" Please note that "" gives you one of the options, not the values. Why? Well, if you think about it you'll realize that select lists and radio groups are fundamentally different from input boxes in a number of ways. Whereas in input tags you can just have an empty value, with lists you need to iterate through each option and then decide if it's selected or not. 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 "" tag and this will all be done automatically, which I strongly recommend. But, let's assume you need exact control over your lists. Here's an example select list template: 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: Notice that the "" tag is expanded to the word "selected" when a given option is present as a value as well (i.e., via the CGI query). The "" tag expands to each option in turn, and "" is expanded to the label for that value. In this case, since "nameopts" was specified to field(), the labels are automatically generated from the options. 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?

Then, we would twiddle our "mailopt" field via field(): $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?

Yes, please keep me on it! No, remove me immediately
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 "fields" loop in your template. This allows you to reuse the same template even if your parameters change. The following template code would loop through each field, creating a table row for each:
Each loop will have a "label", "field", "value", etc, just like above. For more information on templates, see HTML::Template. SEE ALSO CGI::FormBuilder, CGI::FormBuilder::Template, HTML::Template REVISION $Id: HTML.pm 100 2007-03-02 18:13:13Z nwiger $ AUTHOR Copyright (c) Nate Wiger . 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. perl v5.38.0 2023-07-25 CGI::FormBuilder::Template::HTML(3)