I know I have often troubled with the same problems with dynamic <select> tags: they can be a big pain to put together entirely in the template. The HTML::Template FAQ (FAQ #11) suggests using the CGI module's form field generation functions, and simply passing the result to HTML::Template. I like this approach -- on the one hand, you definitely don't need to be bothered with generation of a <select> dropdown in your application logic, but on the other, it's still a disgusting TMPL_LOOP in the template, when logically it should just be one simple TMPL_VAR.

There's no reason the same logic couldn't apply to your radio buttons -- have some facility to simply generate them inside your program (using CGI.pm or otherwise), and just use the results as a string to pass to a TMPL_VAR. Just make sure your application logic doesn't suffer because of it, i.e., still keep the HTML generation far away from the application logic. I swear I've seen threads about this very subject (with regard to <select> tags), but haven't been able to locate them. You may have better luck with your search terms.

Assuming it's feasable to alter the templates you currently have in use to facilitate another TMPL_* tag, a very slick way to do this would be to wrap your HTML::Template calls in a pre-parser that will make the following simple translation:

# change this in your template: <TMPL_RADIO PARAM="foo1" NAME="foo" VALUE="1"> <TMPL_RADIO PARAM="foo2" NAME="foo" VALUE="2"> # to this: <input type=radio name="foo" value="1" <TMPL_IF NAME="foo1">checked</TMPL_IF>> <input type=radio name="foo" value="2" <TMPL_IF NAME="foo2">checked</TMPL_IF>>
Notice the addition of TMPL_RADIO. Take a gander at the filter argument to HTML::Template->new, which would allow you to do just this. Now when you (or your designers) edit the templates, they see one atomic TMPL_* tag without the ugliness of the TMPL_IF, the application code shouldn't need to be changed, and the nested tags don't show up until right before the HTML is passed to HTML::Template.

Best of luck,

blokhead


In reply to Re: HTML::Template and checking arbitrary radio buttons by blokhead
in thread HTML::Template and checking arbitrary radio buttons by dws

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.