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.


In reply to Browser-viewable HTML::Template templates 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.