hesco has asked for the wisdom of the Perl Monks concerning the following question:

I have an application which presents a web form to the user which provides for a variable number fieldsets full of the same fields over and over. I'm using CGI::FormBuilder to render the form, and so far that seems to be working just fine. Now I'm trying to introduce an HTML::Template into the mix.

The CGI::FormBuilder perldoc suggests using something like this:

<p>Your email address: <tmpl_var field-email>
in the html.tmpl file. But I need something more like:
<fieldset class="fb_set" id="person0798"> <p>Your email address: <tmpl_var field-person0798_email> </fieldset> <fieldset class="fb_set" id="person8635"> <p>Your email address: <tmpl_var field-person8635_email> </fieldset> <fieldset class="fb_set" id="person3248"> <p>Your email address: <tmpl_var field-person3248_email> </fieldset>
With the person#### codes being used to interact with the database on the processing side of things. And a bunch of other fields included in each fieldset.

I see no example in the CGI::FormBuilder perldoc of use'ing HTML::Template. Nor of instantiating a $template object, which I'm assuming may all be handled inside the CGI::FormBuilder module, but that is just a guess. I certainly see nothing of the $template->methods which permit passing data to the template. Am I limited only to pass field definitions? And if so, how do I account for these fieldsets of uniquely identified sets of fields?

Am I missing something here?

Confused on how to proceed,

-- Hugh

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Questions on how to use CGI::FormBuilder and HTML::Template together.
by Anonymous Monk on Jun 06, 2008 at 08:55 UTC
Re: Questions on how to use CGI::FormBuilder and HTML::Template together.
by monarch (Priest) on Jun 06, 2008 at 11:07 UTC
    Try a template with the following contents (note that HTML escaping ensures that your variable contents displays correctly):
    <!-- TMPL_LOOP NAME='myloop' --> <fieldset class="fb_set" id="person<!-- TMPL_VAR NAME='id' -->"> <p>Your email address: <!-- TMPL_VAR NAME='email' ESCAPE='html' --> </fieldset> <!-- /TMPL_LOOP -->

    Now, you need to get id and email references into an array for the template. Let's assume you have a hash called people that is indexed by id. So you'd write Perl code like this:

    my $tmpl = HTML::Template->new( ... ); my @loopvar = (); foreach my $id ( keys %people ) { my $email = $people{$id}->{email}; my %rowhash = ( id => $id, email => $email, ); push( @loopvar, \%rowhash ); } $tmpl->param( myloop => \@loopvar );
Re: Questions on how to use CGI::FormBuilder and HTML::Template together.
by Anonymous Monk on Jun 06, 2008 at 08:51 UTC