Ok, I could get the desired output using HTML::Template's <TMPL_LOOP
name=NAME> style .
Hopefully someone can come up with an implementation using <TMPL_LOOP
loop-[name]> of CGI::FB.
Anyways, here is a working code in case someone else gets into the
same hole as i did .. lol :
#!c:/Perl/bin/perl.exe -w
use strict;
use CGI;
use CGI::FormBuilder;
my $q = new CGI;
my $form = CGI::FormBuilder->new(
template => {
type => 'HTML',
filename=> '../../tmpl/test/test_formbuilder_6.tmpl',
associate => $q,
loop_context_vars => 1,
},
header => 1,
);
my $headers = [
{ 'LINK' => ''},
{ URL => $q->script_name . "?sort=BOX",
'LINK' => 'Toy Box'},
{ URL => $q->script_name . "?sort=QTY",
'LINK' => '# of Toys'},
];
# In a real world, I would pull the data from a database and populate
$rows below. For now, using prepopulated test data to show the
structure.
my $rows = [
{'BOXID' => 'B1',
'ODD' => [
{ 'VALUE' => 'BOX001'},
{ 'VALUE' => '10'},
]
},
{'BOXID' => 'B2',
'EVEN' => [
{ 'VALUE' => 'BOX002'},
{ 'VALUE' => '20'},
]
},
{'BOXID' => 'B3',
'CHECKED' => 'checked',
'ODD' => [
{ 'VALUE' => 'BOX003'},
{ 'VALUE' => '30'},
]
},
];
$form->tmpl_param (HEADERS => $headers);
$form->tmpl_param (ROWS => $rows);
$form->field( name => 'boxlist',
value => 'B3',
);
if ($form->submitted) {
print $q->header;
my @fields = $form->field;
print "Got these fields : @fields<br>";
for (@fields) {
print "Value of field '$_' = " . join(',', $form->field
($_)."<br>");
}
} else {
print $form->render(header => 1);
}
Here is the Template file:
<head>
<title>
foo.com</title>
<!-- tmpl_var js-head --> <!-- JavaScript validation code -->
<head>
<body style="background-color: rgb(234, 253, 255); color: rgb(0, 0,
0);" alink="#000099" link="#000099" vlink="#990099">
<p>
Hi! Welcome to
foo.com!
<!-- tmpl_var form-start -->
<p>Select a Toy box:
<table>
<tr>
<!-- TMPL_LOOP NAME=HEADERS -->
<th><a href="<TMPL_VAR NAME=URL>"><!-- TMPL_VAR NAME=LINK --></
a></th>
<!-- /TMPL_LOOP -->
</tr>
<!-- TMPL_LOOP NAME=ROWS -->
<tr>
<!-- TMPL_UNLESS NAME="__ODD__" -->
<td style="background: #B3B3B3">
<input type="radio" name="boxlist" value="<tmpl_var
name=BOXID>" <!-- tmpl_var name=CHECKED -->>
</td>
<!-- TMPL_LOOP NAME=EVEN -->
<td style="background: #B3B3B3">
<!-- TMPL_VAR NAME=VALUE -->
</td>
<!-- /TMPL_LOOP -->
<!-- TMPL_ELSE -->
<td style="background: #CCCCCC">
<input type="radio" name="boxlist" value="<tmpl_var
name=BOXID>" <!-- tmpl_var name=CHECKED -->>
</td>
<!-- TMPL_LOOP NAME=ODD -->
<td style="background: #CCCCCC">
<!-- TMPL_VAR NAME=VALUE -->
</td>
<!-- /TMPL_LOOP -->
<!-- /TMPL_UNLESS -->
</tr>
<!-- /TMPL_LOOP -->
</table>
<p>
When you're done, hit the "Submit" button below:<br>
<!-- tmpl_var form-reset --> <!-- tmpl_var form-submit --> <!--
buttons -->
<!-- tmpl_var form-end -->