http://qs321.pair.com?node_id=226148

I while back I posed a question about handling checkboxes and radio buttons in HTML::Template templates. I was looking for a scheme that de-coupled page design from application development, so that both could proceed in parallel. In particular, I wanted to be able to view templates directly in a browser (to verify design changes), while also supporting the ability to generate a page with specific radio boxes and check boxes checked.

A first glance, there are three approaches to generating pages that have specific boxes checked.

One either has to produce the HTML input tags in code, via
$template->param(foo_input => "<input type=checkbox name='foo'" . $foo_checked ? " checked>" : + ">"); ... <TMPL_VAR foo_input>
or one has to temporarily break the HTML in the template by doing
$template->PARAM(foo_checked => $foo_checked); ... <input type=checkbox name="foo" <TMPL_IF foo_checked>checked</TMPL_I +F>>
or one has to bloat the template with conditional logic
$template->param(foo_checked => $foo_checked); ... <TMPL_IF foo_checked> <input type=checkbox name="foo" checked> </TMPL_ELSE> <input type=checkbox name="foo"> </TMPL_IF>
All of these approaches work, but none allow the unexpanded template to be viewed in a browser. In the first scheme you get an empty hole, the second scheme results in invalid HTML, and the third scheme shows each input control twice.

I didn't find a clean scheme, but did settle on a hybrid one that works.

When the initial page design is templatized,

<input type=checkbox name="foo">
gets turned into
<TMPL_IF false><input type=checkbox name="foo"></TMPL_IF><TMPL_VAR f +oo_input>
with   $template->param(false => 0); When viewed in a browser, the original input tag is displayed. But when the template is expanded, the tag in the template gets discarded and a dynamically generated tag takes its place.

The downsides of this scheme are that HTML is being produced in code, and that template designers need to coordinate changes with the application developers (which is true anyway). But it works, and it also works when generating dynamic pop-up menus using select tags.

If anyone has a better approach, I would love to hear about it.