Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

HTML::Template also makes it easy and clean to make other form elements sticky. it lets you just associate the CGI object with the template:

my $query = new CGI; my $template = HTML::Template->new(filename => 'template.tmpl', associate => $query);

and a template variable is set for every param that CGI knows about.

for selects, i still don't like to use $query->popup_menu. instead i have a function in a standard library that i use for all my web programming like so:

sub selectify { my $values = shift; my $labels = shift; my $selected = shift; my %selected = map {$_ => 1} @{$selected}; return [map { { value => $_, label => shift @{$labels}, selected => $selected{$_} || "", } } @{$values}]; }

so in my code i can do:

$template->param(some_loop => selectify(\@values,\@labels,[$query->par +am('some_param')]);

and in the template:

<select name="some_param"> <tmpl_loop name="some_loop"> <tmpl_include name="inner_loop.tmpl"> </tmpl_loop> </select>

inner_loop.tmpl is factored out into its own template so i can reuse it all over the place. it just contains:

<option value="<tmpl_var name="value" escape="html">"<tmpl_if name="se +lected"> selected="selected"</tmpl_if>><tmpl_var name="label" escape= +"html"></option>

i don't know, i guess i just can't stand the thought of any of my output html being generated by and not totally controlled by my templates. that way if the designer wants to add a size, or class attribute to the select, they don't have to ask me to add it to the perl code.

anders pearson

In reply to Re: Re: Nirvana through the templating yin yang (TT2 / by thraxil
in thread Nirvana through the templating yin yang (TT2 / by Aristotle

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2015-12-01 06:09 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (794 votes), past polls