Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Populating options in html select.

by techcode (Hermit)
on Sep 28, 2005 at 22:57 UTC ( [id://495936]=perlquestion: print w/replies, xml ) Need Help??

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

Yeah I know - it's asked and answered 100's of times. But I'm not happy with proposed solutions.

I know about HTML::FillInForm and other modules with similar functionality. But I haven't found any that would beside selecting right option in a <select> also dynamically make those.

To make it more clear. Inside my template (HTML::Template)

<select name="whatever"> Insert possible options here and of course select the right one </select>
I know about solution with CGI.pm that would generate the whole thing together with select but I use CGI::Simple - and I want to leave it possible for designer (or whoever) to say set class (CSS) for the particular select - or maybe add onChange JS event handler ...

Another solution would be :

<select name="whatever"> <!-- TMPL_VAR NAME="whatever_select" --> </select>
But then I end up with HTML generating code - which means I have html code inside my Perl code ...
<select name="whatever"> <!-- TMPL_LOOP NAME="whatever_select" --> <option value="<!-- TMPL_VAR NAME="id" -->" <!-- TMPL_VAR NAME="checke +d" -->><!-- TMPL_VAR NAME="value" --></option> <!-- /TMPL_LOOP --> </select>
Also looks kinda funky :) And it also requires work inside my code.

I was thinking of simply passing array(ref) of possible values that would be used for filling options. And then HTML::FillInForm would easily select right option. Any module that does that? If not, I might try to write it myself - but I'm not that good with parsing ...

Have you tried freelancing? Check out Scriptlance - I work there.

Replies are listed 'Best First'.
Re: Populating options in html select.
by wfsp (Abbot) on Sep 29, 2005 at 04:33 UTC
    I would still go with HTML::Template.

    You could change how it looked:

    <select name="whatever"> <!-- TMPL_LOOP NAME="whatever_select" --> <option value="<!-- TMPL_VAR NAME="id" -->" <!-- TMPL_VAR NAME="checked" --> > <!-- TMPL_VAR NAME="value" --> </option> <!-- /TMPL_LOOP --> </select>
    This would need an array ref anyway.
    $array_ref = [ { id => id1, checked => 'selected', value => 'one', }, { id => id2, checked => '', value => 'two', }, { id => id3, checked => '', value => 'three', }, ];
Re: Populating options in html select.
by jbrugger (Parson) on Sep 29, 2005 at 04:34 UTC
    Another idea is to use HTML::Template::Expr and do something like this:
    <option value="<!--TMPL_VAR NAME=yourOptionValue -->" <!--TMPL_IF +EXPR="yourPerlSelectFunction()" -->selected<!--/TMPL_IF -->> <!--TMPL +_VAR NAME=yourOptionText --></option>
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Populating options in html select.
by Zed_Lopez (Chaplain) on Sep 29, 2005 at 06:26 UTC

    Here's advice on using CGI::FormBuilder with HTML::Template for this.

    I've also done it by passing another template variable whose value is the selected option, and having a TMPL_IF in my TMPL_LOOP comparing each value to the selected option. That left a bad taste in my mouth, but it worked.

Re: Populating options in html select.
by techcode (Hermit) on Sep 29, 2005 at 14:48 UTC
    What the tree of you suggested so far are basically just a bit modified solutions of those that I already wrote.

    I would feel much nicer to make a module that would do it for me (and everyone else). I guess that it would just implement that function, and reuse everything from HTML::FillInForm or similar (HTML::Defaultify) by inheritance. Or be included in it ...

    Speaking of letting all to be done by the module, wouldn't it be nice to just give it a name of table (fields : id = option value; text = option text) possibly a DBH and let all to be done for you ?


    Have you tried freelancing? Check out Scriptlance - I work there.
Re: Populating options in html select.
by rhesa (Vicar) on Oct 11, 2005 at 12:41 UTC
    If your main concern is freedom for the designer, then using your third solution (with the html_loop) is by far the best. No other solution gives the designer the option to switch to a list of checkboxes or radio buttons, or style individual options.

    Here's an example of a styled select box:

    <html> <style> .even { background-color: #ccc } .odd { font-weight: bold } </style> <body> <form> <select> <option class=odd> one <option class=even> two <option class=odd> one <option class=even> two <option class=odd> one <option class=even> two </select> </form> </body> </html>

    You could use HTML::Template's __odd__ and __even__ attributes for this, for example. But there's no way to do this with your FillInForm subclass idea.

Re: Populating options in html select.
by NetWallah (Canon) on Nov 03, 2005 at 21:37 UTC
    To help future searchers on this info make better choices, here is a new, and IMHO, ultimate and preferred solution to this issue :

    HTML::Menu::Select.

         "Man cannot live by bread alone...
             He'd better have some goat cheese and wine to go with it!"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found