Has anyone come up with a clean way of checking arbitrary radio box inputs when using HTML::Template?

I'm using template to build a set of semi-complicated pages. Each page holds a form. When submitted, validation logic checks the consistency of the data that comes in via text fields, radio buttons, and a stray checkbox. If validation fails, I reissue the page with all of the input boxes filled in as they were, and a pithy message at the top explaining what's wrong.

Trying to maintin input state leads to a messy problem; one that I'm hoping someone has come up with a clean approach to.

Regenerating text fields is easy. The templates say

<input type=text name=foo value='<TMPL_VAR NAME="foo">'>
and as long as I've given the template a parameter binding for "foo", things work fine. A major plus is that I can view the template in a browser to verify formatting.

Radio buttons are a messier. I want to leave as much clean HTML in the template as possible. But how to regenerate them such that I don't lose the selection? I could do violence to the HTML, and write something like:

<input type=radio name=bar value="bar1" <TMPL_VAR NAME="bar1_checked">>
where "bar1_checked" will either be "" or "checked". But this screws up the HTML in the template, rendering it unviewable.

The other approach is to do something like

<TMPL_IF NAME="bar1_checked"> <input type=radio name=bar value="bar1" checked> <TMPL_ELSE> <input type=radio name=bar value="bar1"> </TMPL_IF>
around each radio box. The template is then viewable as HTML, though each box appears twice, which screws up formatting. To keep tables from getting screwed up, I resorted to writing
<TMPL_IF NAME="bar1_checked"> <tr> <td><input type=radio name=bar value="bar1" checked></td> <td>$47</td> </tr> <TMPL_ELSE> <tr> <td><input type=radio name=bar value="bar1"></td> <td>$47</td> </tr> </TMPL_IF>
but this bloated my templates something aweful, and requires that I invent a new template parameter per radio box. With a dozen radio boxes, the space ads up.

Does anyone know of a clean approach that will let me generate a page with arbitrary radio buttons checked, without having to break or bloat the HTML? Or am I S.O.L.?

P.S. JavaScript is not on option.


In reply to 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.