Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Maintaining form state with HTML::Template

by dwiz (Pilgrim)
on Jun 22, 2001 at 18:02 UTC ( [id://90715]=perlquestion: print w/replies, xml ) Need Help??

dwiz has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I would like some help please with maintaining form state using HTML::Template.

My efforts thus far have been unspectacular to say the least. Maintaining the state with checkboxes is easy but when it comes to dropdown menus it has become tricky. With the dropdowns it easy to generate using my script like:

sub StateDrop { my $state = $q->param('state'); my @states = ("1", "2", "3", "4", "5", "6"); my $menu .= qq"<SELECT NAME="state">\n"; foreach my $x (@states) { $menu .= $x eq $state ? "<option selected>$x<\/OPTION>" : "<OPTION>$x< +\/OPTION>"; } $menu .= "<\/SELECT>"; return $menu; }
I then place that in my Template file via its param method.
But that defeats the purpose of using a Template file. If a change needs to be made then I need to edit the script its self.
Is there a way to this so that it adapts no matter what is in the dropdown menu?
Thanks for any help.

Replies are listed 'Best First'.
Re: Maintaining form state with HTML::Template
by Arguile (Hermit) on Jun 22, 2001 at 18:28 UTC

    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.

    Update:

    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 );
      I agree with what you are saying but with the TMPL_LOOP I have to supply the vars script side.
      I want to avoid that all together thats why I was looking for a better way.
      -dwiz
        There may be a better way, but we use the same technique Arguile presented.

        The only other method we tried was to use JavaScript to set the selected option based on another input. Nice if you like JavaScript, but I prefer the <TMPL_LOOP> version as it doesn't depend on what the browser does with the JS code.

(jeffa) Re: Maintaining form state with HTML::Template
by jeffa (Bishop) on Jun 22, 2001 at 18:24 UTC
    I highly recommend Apache::Session, you can either store the state info the database of your choice or DBM files.

    You can shove all of your information into one hash, then use Data::Dumper or Storable to 'pack' and 'unpack' your hash.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Maintaining form state with HTML::Template
by miyagawa (Chaplain) on Jun 22, 2001 at 20:45 UTC
    HTML::FillInForm will do it. First output the HTML::Template, then fill in the form (make sticky form) with HTML::FillInForm.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://90715]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found