in reply to Maintaining form state with HTML::Template
You're violating the entire principle of HTML::Template in that code, that module is about removing all HTML from the perl script :). If you want to embed the HTML, I'd suggest you look at Template::Toolkit, HTML::Mason, or embed_perl which are all execellent modules.
With HTML::Template you'd want to use a <TMPL_LOOP ...> in your template file to generate the options. There are a few ways to approach the "selected", the easiest would be to also have a <TMPL_IF ...>selected</TMPL_IF> in the template triggered by the comparison in the script.
There's an HTML::Template help list you can subscribe to listed at the end of the POD for the module. Also check out the others listed, TMTOWTDI and YMMV with each.
As requested, a sample of what you'd want to do with this mod.
<!-- foo.tmpl --> ... <select name="dropdown"> <tmpl_loop name="menu"> <option value="<tmpl_var name="item">" <tmpl_if name="selected">se +lected</tmpl_if>><tmpl_var name="item"> </tmpl_loop> </select> ...
### foo.pl ### my @menu; push @menu, { item => $_, selected => $_ eq $state } for 1..6; $t->param( menu => \@menu );
I love the idea of the module though the implementation can get a little yucky (tags in tags). I've been mucking around inside the module to play with different techniques for subbing in vars and you can customise it quite a bit (right now I'm mainly just suceeding in breaking it though :). Anyways, just incase that's too condenced for you I expanded the script part a bit (longer version not tested).
my @nums = (1..6); my (@menu, %option, $selected); foreach $x (@nums) { # Set selected to true if the state equals the current item. if ($state eq $x) { $selected = 1; } else { $selected = 0; } # Tell the option the item value and whether it's selected. my %option = { item => $x, selected => $selected }; # Put the option in the menu. push @menu, \%option; } # Send the menu to the template; $t->param( menu => \@menu );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Maintaining form state with HTML::Template
by dwiz (Pilgrim) on Jun 22, 2001 at 18:45 UTC | |
by TheoPetersen (Priest) on Jun 22, 2001 at 19:39 UTC |