Sibling monks - I crave your indulgence - I have two ways to do a thing, neither of which quite hits the spot. Is there a Third Way?

I'm outputting a CGI form via HTML::Template. Sometimes it's a blank form, sometimes it has values filled in (because the user selected them in an earlier session). Now, I have a lot of drop-down (or pop-up if you're a CGI.pm buff) menus. To populate those menus with values using HTML::Template I can't see any alternative to having a series of <TMPL_IF> statements, like:
<select name="MinDuration"> <option value=0<TMPL_IF MINDURATION0> selected</TMPL_IF>> any <option value=1<TMPL_IF MINDURATION1> selected</TMPL_IF>> one +day <option value=2<TMPL_IF MINDURATION2> selected</TMPL_IF>> two +days <option value=3<TMPL_IF MINDURATION3> selected</TMPL_IF>> thre +e days <option value=4<TMPL_IF MINDURATION4> selected</TMPL_IF>> four + days <option value=5<TMPL_IF MINDURATION5> selected</TMPL_IF>> one +week </select>
But that means (AFAIK) that in the hash which I feed to my template I have to have lots ofkeys that don't do anything - i.e. to select "3 days":
$my_template->param( { minduration0 => 0, minduration1 => 0, minduration2 => 0, minduration3 => 1, minduration4 => 0, minduration5 => 0, } );
Plus, this is ugly because there is no direct relationship between the returned value and the value I feed to my template.

The alternative I see is to use CGI.pm to generate the whole menu. Then instead of the html above I'd just have
<TMPL_VAR DATEMENU>
And then in the script I'd have
$my_template->param( { datemenu => popup_menu( -name => 'MinDuration', -values => [0..5], -default => 3, -labels => { 0 => ' any', 1 => ' one day', 2 => ' two days', 3 => ' three days', 4 => ' four days', 5 => ' one week', } ),
But neither of these is quite right. The first is too cludgy in translating from submitted values to initialisation values; the seconde uses all the CGI.pm magic, but it's a two step, and it involves sending large chunks of html to HTML::Template which... feels a bit wrong. Left to my own devices I'd go with method two as the lesser evil, but I wonder if there is a cleaner way - either some clever HTML::Template wheeze, or a different templating system or... TIMTOWTDI???

§ George Sherston

In reply to SELECT pop-up menus in HTML::Template by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.