You're violating the entire principle of HTML::Template in that code, that module is about removing all HTML from the perl script :). If you want to embed the HTML, I'd suggest you look at Template::Toolkit, HTML::Mason, or embed_perl which are all execellent modules.

With HTML::Template you'd want to use a <TMPL_LOOP ...> in your template file to generate the options. There are a few ways to approach the "selected", the easiest would be to also have a <TMPL_IF ...>selected</TMPL_IF> in the template triggered by the comparison in the script.

There's an HTML::Template help list you can subscribe to listed at the end of the POD for the module. Also check out the others listed, TMTOWTDI and YMMV with each.

Update:

As requested, a sample of what you'd want to do with this mod.

<!-- foo.tmpl --> ... <select name="dropdown"> <tmpl_loop name="menu"> <option value="<tmpl_var name="item">" <tmpl_if name="selected">se +lected</tmpl_if>><tmpl_var name="item"> </tmpl_loop> </select> ...
### foo.pl ### my @menu; push @menu, { item => $_, selected => $_ eq $state } for 1..6; $t->param( menu => \@menu );

I love the idea of the module though the implementation can get a little yucky (tags in tags). I've been mucking around inside the module to play with different techniques for subbing in vars and you can customise it quite a bit (right now I'm mainly just suceeding in breaking it though :). Anyways, just incase that's too condenced for you I expanded the script part a bit (longer version not tested).

my @nums = (1..6); my (@menu, %option, $selected); foreach $x (@nums) { # Set selected to true if the state equals the current item. if ($state eq $x) { $selected = 1; } else { $selected = 0; } # Tell the option the item value and whether it's selected. my %option = { item => $x, selected => $selected }; # Put the option in the menu. push @menu, \%option; } # Send the menu to the template; $t->param( menu => \@menu );

In reply to Re: Maintaining form state with HTML::Template by Arguile
in thread Maintaining form state with HTML::Template by dwiz

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.