<tmpl_loop loop-name> verses <tmpl_loop name=NAME> CGI::FB + HTML::Template

19 views
Skip to first unread message

rc_tronix

unread,
Aug 13, 2009, 3:55:39 PM8/13/09
to perl-formbuilder
Using CGI::FB + HTML::Template inside CGI::Application and loving it.

I have used the <tmpl_loop loop-name> and <tmpl_loop name=NAME>
seperately , but never together and was wondering if someone can post
an example code and template that shows how it can be used together-
one inside the other.

I'm inspired by these articles:
http://www.formbuilder.org/tutor/index.pl?c=3&s=4 .
http://www.perlmonks.org/?node_id=65642 .

Primarily what I am trying to achieve here is this (fictituos
requirement):
I have in my database, boxnames and a quantity of items in each box. I
need to present a form using a table which displays, in each row, a
radio button, the boxname and the quatity . Using <tmpl_loop loop-
[name]>, I am able to get the radio button and the boxname in the same
row, but can't figure out a datastructure to get the quantity
alongside in the third column. ( I don't want to display "BOX001
having # items" as a string in the 2nd column; I need the qty to be in
the 3rd column by itself ).

Pulling data from the database, I populate this struct:
<code>

my $boxdata = [
['B1' => 'BOX001'], # value => label
['B2' => 'BOX002'], # value => label
['B3' => 'BOX003'],
];
$form->field(name => 'boxlist', # corrosponds to <tmpl_loop loop-
boxlist> in the tmpl file.
options => $boxdata,
value => 'B2',
);

In the tmpl file:
<!-- loop through BOX options -->
<table border='0'>
<tmpl_loop loop-boxlist>
<tr>
<td style="background: #B3B3B3">
<input type="radio" name="box" value="<tmpl_var value>"
<tmpl_var checked>>
</td>
<td style="background: #CCCCCC"><tmpl_var label>
</td>
</tr>
</tmpl_loop>
</table>

</code>
I'm thinking I'll probably need a <tmpl_var name=loopname> between the
<tr> and </tr> and populate using
$form->tmpl_param(loopname=>\@somestructure) for looping through
additional columns.

I'm stuck .... any pointers will be highly appreciated.

rc_tronix

unread,
Aug 13, 2009, 8:54:58 PM8/13/09
to perl-formbuilder
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 -->

Steve Knoblock

unread,
Aug 14, 2009, 1:05:15 PM8/14/09
to perl-for...@googlegroups.com
On Thu, 13 Aug 2009 17:54:58 -0700 (PDT), you wrote:

>
>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 :

I've been in a lot of holes with CFB and HTML template. FB tends to
structure and limit what you can do as does template in ways the
author intended but perhaps not every situation. CFB has evolved to
become much more powerful and flexible. It is fantastic the way the
perl modules just know about each other and cooperate (thanks to the
authors) but sometimes you get so close and there is nothing to bridge
the gap to what you want. I wish I remembered enough to help, off the
top of my head, but you have my sympathy.

Steve

Steve Knoblock
Back to our regularly scheduled programming...for(i=0;i<=10;i++) { print i }
work: http://folkstreams.net
contact: http://brandymorecastle.org edi...@city-gallery.com

Reply all
Reply to author
Forward
0 new messages