On Fri, Oct 29, 2004 at 01:39:32PM -0400, Buddy Burden wrote:
> Rod,
> 
> >I have several input in a for that are T/F, Y/N, etc. and am getting the 
> >defaults from a database lookup before creating the form.  Is there better 
> >(ie. more TT2 or perlish) way than doing this way?
> 
> Well, I don't know that it's particularly more Perl-ish or more TT2, but 
> I always do it this way:
> 
>    <input type="radio" name="GETS_REPORT" value="F"
>      [%- 'CHECKED' IF gets_report == 'F' %]/>No
>    <input type="radio" name="GETS_REPORT" value="T"
>      [%- 'CHECKED' IF gets_report == 'T' %]/>Yes
> 

An easier way of doing this is with the CGI module... pass a CGI object (in this case 
'q') to the template and you can do fun stuff like:

[% q.radio_group(name => 'GETS_REPORT',
                        values=>['T','F'],
                        default=>gets_report,
                        labels=>{'T'=>'Yes','F'=>'No'}
                ); %]

If you want to get fancier you make stuff persistent:

[% SET TFlabels = {'T'=>'Yes','F'=>'No'}; %]
[% SET TFvalues = ['T','F']; %]
[% q.radio_group(name => 'GETS_REPORT',
                        values=>TFvalues,
                        default=>q.param('GETS_REPORT'),
                        labels=>TFlabels}
                ); %]

And you can then add more radio groups with the same params dead simply... Like say 
you have an array of questions in a hash with the keys as labels


[% SET TFlabels = {'T'=>'Yes','F'=>'No'}; %]
[% SET TFvalues = ['T','F']; %]
[% FOREACH question = questions.keys %]
        [% q.radio_group(name => question,
                        values=>TFvalues,
                        default=>q.param(questions.$question),
                        labels=>TFlabels}
                ); %]
[% END %]

VOILA!

BTW... I have noticed that most of the people asking questions and showing DBI 
examples and such seem to hardcode their HTML... am I doing something evil by using 
the CGI module for all this?

For me at least I love the fact that it handles all the formatting.. keeps the html 
100% standards compliant and generates things like popup groups and multi selects with 
ease.

Is there some big performance hit that I am missing? :}

-- 
Todd Freeman  Ext 6103                   .^.    Don't fear the penguins!
Programming Department                   /V\
Andrews University                      // \\    http://www.linux.org/
http://www.andrews.edu/~freeman/       /(   )\   http://www.debian.org/
                                        ^^ ^^

Attachment: signature.asc
Description: Digital signature

Reply via email to