Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: HTML::Template recalling selected dropdown options

by Hero Zzyzzx (Curate)
on May 12, 2005 at 03:01 UTC ( #456218=note: print w/replies, xml ) Need Help??

in reply to HTML::Template recalling selected dropdown options

The HTML form generation features of CGI will save your butt here, the popup_menu() method will remember the last selected value by default. The "sticky value" behavior is very cool.

my $menu=$q->popup_menu( -name=>"templates", -id=>"templates", -values=>["",23,36], -labels=>{""=>"Select . . .",23=>"Text One",36=>"Text Two" +} );

You should also check out CGI::FormBuilder, which can automate a whole slew of form generation stuff, and use HTML::Template for output control.

-Any sufficiently advanced technology is
indistinguishable from doubletalk.

My Biz

Replies are listed 'Best First'.
Re^2: HTML::Template recalling selected dropdown options
by bradcathey (Prior) on May 12, 2005 at 12:24 UTC

    Thanks, I did consider this, but I have this "thing" about keeping as much of the HTML as possible on the HTML side (but even Sam himself doesn't have this hang-up, and I quote from the H::T doc:

    "To some people this smacks of mixing HTML and code in a way that they hoped HTML::Template would help them avoid. To them Iíd say that HTML is a violation of the principle of separating design from programming. Thereís no clear separation between the programmatic elements of the <form> tags and the layout of the <form> tags. Youíll have to draw the line somewhere - clearly the designer canít be entirely in charge of form creation. Itís a balancing act and you have to weigh the pros and cons on each side. It is certainly possible to produce a <select> element entirely inside the template. What you end up with is a ratís nest of loops and conditionals. Alternately you can give up a certain amount of flexibility in return for vastly simplifying your templates. I generally choose the latter."

    I guess if popup_menu() can help me avoid the mess of javascript/html/Perl then it might be worth a closer look.

    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      popup_menu() (and checkbox_group(), radio_group(), etc) can absolutely simplify your templates, on large forms you'll see a huge difference. Personally I haven't often had the need to edit form fields directly as HTML, because you can apply a class to the field and change much of it's appearance with CSS. Most field options are created off database queries, seldom are select/radio/check lists hardcoded.

      I only use to parse values, print headers, handle uploads and create HTML Forms (though I've started to use CGI::FormBuilder for most of my forms). On trivial apps I'll use the HTML generation features, too. For me, I think this is's "highest and best" use- the tricky stuff that doesn't usually impact design is handled by a library that does it EXTREMELY well.

      The way I think of it- you can absolutely save yourself time generating the HTML form fields with CGI, and in those cases where you just can't because of some design problem (which is pretty rare, in my experience), you can always fall back to manual form element generation. In the meantime, you've still come out ahead, with smart forms and saved effort.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://456218]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2021-04-20 16:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found