in reply to HTML::Template and checking arbitrary radio buttons

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