in reply to HTML::Template, associate 'method', and popup menus

The value for USERSTATE is being passed as normal, just like for any other single input box. The problem is that YOU have tell which item from the select box was selected. No matter how you go about it, you will have to loop through every item that will be in the select box and see if it is the one the user selected. If so, then you need pass a "flag" to let the template know to add the magic selected=1 attribute to the proper <option> tag.

And that's why a lot of folks simply let CGI.pm figure that out for them. When you use CGI.pm's popup_menu or scrolling_list methods, CGI.pm will mark the proper <option> tag with the selected=1 attribute. The following example uses Geography::States instead of a database. The first part of the code simply munges the results from Geography::States into a datastructure that is suitable for an HTML::Template <tmpl_loop> -- but instead i pass that off to CGI.pm's scrolling_list method, hence the maps. You can test this code out at [http://unlocalhost.com/cgi-bin/select.cgi]

use strict; use warnings; use Data::Dumper; use HTML::Template; use Geography::States; use CGI qw(:standard); my @state; my $usa = Geography::States->new('USA'); push @state,$_ for $usa->state; my $state = [ map {{ id => $_->[0], name => $_->[1] }} sort { $a->[1] cmp $b->[1] } @state ]; my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(states => scrolling_list( -name => 'states', -values => [ map $_->{id}, @$state ], -size => 12, -multiple => 'why not', -labels => { map {$_->{id} => $_->{name}} @$state }, ), ); print header,$tmpl->output; __DATA__ <form> <tmpl_var states> <p><input type="submit" /></p> </form>
The other option is to loop through the select items yourself and mark which one was selected. Then you need a 'not-so-elegant' template like the following:
<select name="states"> <tmpl_loop states> <option value="<tmpl_var value>" <tmpl_if selected>selected="1 +"</tmpl_if> > <tmpl_var label> </option> </tmpl_loop> </select>
Since you are already using CGI.pm ... why not let it do the hard work of making the select boxes for you as well?

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: HTML::Template, associate 'method', and popup menus
by geektron (Curate) on Oct 15, 2004 at 00:52 UTC
    OK, so i did misunderstand the magick of the associate call. fair 'nuf.

    what i *did* learn from this, is that the Geography::States module exists, so i don't have to hand-code those.

    I always used to let  CGI handle my form stuff, until I got stuck with  HTML::Template at work. before then, i always used the  CGI extensions from  Template::Toolkit.

    i genuinely *hadn't* thought about using  CGI's form methods to pass to the template ... now, since my PHB also pokes around in the templates, i wonder how much i'll confuse him if i switch to this way ...