.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "CGI::FormBuilder::Template::HTML 3" .TH CGI::FormBuilder::Template::HTML 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME CGI::FormBuilder::Template::HTML \- FormBuilder interface to HTML::Template .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 4 \& my $form = CGI::FormBuilder\->new( \& fields => \e@fields, \& template => \*(Aqform.tmpl\*(Aq, \& ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This engine adapts \fBFormBuilder\fR to use \f(CW\*(C`HTML::Template\*(C'\fR. \&\f(CW\*(C`HTML::Template\*(C'\fR is the default template option and is activated one of two ways. Either: .PP .Vb 4 \& my $form = CGI::FormBuilder\->new( \& fields => \e@fields, \& template => \*(Aqform.tmpl\*(Aq, \& ); .Ve .PP Or, you can specify any options which \f(CW\*(C`HTML::Template\->new\*(C'\fR accepts by using a hashref: .PP .Vb 9 \& my $form = CGI::FormBuilder\->new( \& fields => \e@fields, \& template => { \& type => \*(AqHTML\*(Aq, \& filename => \*(Aqform.tmpl\*(Aq, \& shared_cache => 1, \& loop_context_vars => 1 \& } \& ); .Ve .PP The following methods are provided (usually only used internally): .SS engine .IX Subsection "engine" Returns a reference to the \f(CW\*(C`HTML::Template\*(C'\fR object .SS prepare .IX Subsection "prepare" Returns a hash of all the fields ready to be rendered. .SS render .IX Subsection "render" Uses the prepared hash and expands the template, returning a string of HTML. .SH TEMPLATES .IX Header "TEMPLATES" In your template, each of the form fields will correspond directly to a \f(CW\*(C`\*(C'\fR 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 \f(CW\*(C`\*(C'\fR in your template. .PP In addition, there are a couple special fields: .PP .Vb 6 \& \- 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 .Ve .PP Let's look at an example \f(CW\*(C`form.tmpl\*(C'\fR template we could use: .PP .Vb 10 \& <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: \&

\& \& .Ve .PP As you see, you get a \f(CW\*(C`\*(C'\fR for each for field you define. .PP 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 \f(CW\*(C`\*(C'\fR tags as well: .PP .Vb 5 \& \- The value of a given field \& \- The human\-readable label \& \- Any optional comment \& \- Error text if validation fails \& \- See if the field is required .Ve .PP This means you could say something like this in your template: .PP .Vb 3 \& : \& \& .Ve .PP And \fBFormBuilder\fR would take care of the value stickiness for you, while you have control over the specifics of the \f(CW\*(C`\*(C'\fR tag. A sample expansion may create HTML like the following: .PP .Vb 3 \& Email: \& \& You must enter a valid value .Ve .PP Note, though, that this will only get the \fIfirst\fR 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 \&\f(CW\*(C`\*(C'\fR prefixed with "loop\-". So, if you had: .PP .Vb 1 \& myapp.cgi?color=gray&color=red&color=blue .Ve .PP This would give the \f(CW\*(C`color\*(C'\fR field three values. To create a select list, you would do this in your template: .PP .Vb 5 \& .Ve .PP With \f(CW\*(C`\*(C'\fR tags, each iteration gives you several variables: .PP .Vb 6 \& Inside , this... Gives you this \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& value of that option \& label for that option \& if selected, the word "checked" \& if selected, the word "selected" .Ve .PP Please note that \f(CW\*(C`\*(C'\fR gives you one of the \fIoptions\fR, 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. .PP When you need precise control in a template this is all exposed to you; normally \fBFormBuilder\fR does all this magic for you. If you don't need exact control over your lists, simply use the \f(CW\*(C`\*(C'\fR tag and this will all be done automatically, which I strongly recommend. .PP But, let's assume you need exact control over your lists. Here's an example select list template: .PP .Vb 5 \& .Ve .PP Then, your Perl code would fiddle the field as follows: .PP .Vb 4 \& $form\->field( \& name => \*(Aqcolor\*(Aq, nameopts => 1, \& options => [qw(red green blue yellow black white gray)] \& ); .Ve .PP Assuming query string as shown above, the template would then be expanded to something like this: .PP .Vb 9 \& .Ve .PP Notice that the \f(CW\*(C`\*(C'\fR 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 \f(CW\*(C`\*(C'\fR tag expands to each option in turn, and \f(CW\*(C`\*(C'\fR is expanded to the label for that value. In this case, since \f(CW\*(C`nameopts\*(C'\fR was specified to \f(CWfield()\fR, the labels are automatically generated from the options. .PP 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: .PP .Vb 9 \& Do you want to be on our mailing list? \&

\& \& \& \& \&
\& \&
.Ve .PP Then, we would twiddle our \f(CW\*(C`mailopt\*(C'\fR field via \f(CWfield()\fR: .PP .Vb 7 \& $form\->field( \& name => \*(Aqmailopt\*(Aq, \& options => [ \& [ 1 => \*(AqYes, please keep me on it!\*(Aq ], \& [ 0 => \*(AqNo, remove me immediately.\*(Aq ] \& ] \& ); .Ve .PP When the template is rendered, the result would be something like this: .PP .Vb 2 \& Do you want to be on our mailing list? \&

\& \& \& \& \& \& \& \&
\& \& Yes, please keep me on it! \& \& No, remove me immediately
.Ve .PP When the form was then sumbmitted, you would access the values just like any other field: .PP .Vb 5 \& if ($form\->field(\*(Aqmailopt\*(Aq)) { \& # is 1, so add them \& } else { \& # is 0, remove them \& } .Ve .PP Finally, you can also loop through each of the fields using the top-level \&\f(CW\*(C`fields\*(C'\fR 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: .PP .Vb 8 \& \& \& \& \& \& \& \&
.Ve .PP Each loop will have a \f(CW\*(C`label\*(C'\fR, \f(CW\*(C`field\*(C'\fR, \f(CW\*(C`value\*(C'\fR, etc, just like above. .PP For more information on templates, see HTML::Template. .SH "SEE ALSO" .IX Header "SEE ALSO" CGI::FormBuilder, CGI::FormBuilder::Template, HTML::Template .SH REVISION .IX Header "REVISION" \&\f(CW$Id:\fR HTML.pm 100 2007\-03\-02 18:13:13Z nwiger $ .SH AUTHOR .IX Header "AUTHOR" Copyright (c) Nate Wiger . All Rights Reserved. .PP 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.