Hi Monks,

I've a template (simplified to reduce clutter) that is used for two slightly different html outputs. Basically, in one output, a list of school names is included while in another, it's not. The relevant template and code are as follows:

Updated

# the template named generic.tt <tr><th align="center" colspan="2"><tmpl_var name=des></th> <tr><td><tmpl_var escape=html name=label></td><td><input type="text" n +ame="<tmpl_var escape=html name=field>" size="<tmpl_var escape=html n +ame=value>"></td></tr> <tmpl_if name=if_more> <select name="school_id"><option selected value="1"><option selected v +alue="0">Select<tmpl_loop name=options> <option value="<tmpl_var name=school_id>"><tmpl_var escape=html name=s +chool></option></tmpl_loop> </select></td></tr> </tmpl_if> <tr><td align="center" colspan="2"><input type="submit" name="action" +value="<tmpl_var escape=html name=submit>"></td></td> # template output 1 # prints school names as well # school names are stored in @options # this part works # @options already set prior to setting up the template params my %hash; %hash ( des => 'Form 1', # field label label => 'Label 1', # name of field field => 'field1', # size of text input field value => '5', submit => 'submit', if_more => 'true', # list of schools options => \@options, ); # call generate to output the template generate(\%hash); # template output 2 # don't print school names %hash ( des => 'Form 2', # field label label => 'Label 2', # name of field field => 'field2', # size of text input field value => '10', submit => 'submit', if_more => 0, ); generate(\%hash); sub generate { my $hashref = shift; my $template = HTML::Template -> new(filename => "$tmpldir/generic.t +t"); $template ->param( des => $hashref->{'des'}, form => $hashref->{'form'}, action => $hashref->{'action'}, label => $hashref->{'label'}, field => $hashref->{'field'}, value => $hashref->{'value'}, submit => 'submit', if_more => $hashref->{'if_more'}, options => $hashref->{'options'}, ); print $template->output; }
The first template output (the one that prints the list of schools in @options) works fine

With the second template output, I get this error HTML::Template::param() : attempt to set parameter 'options' with a scalar - parameter is not a TMPL_VAR! at test.pl line 117

The error goes away if I include an empty option list as follows:

# template output 2 (changed) # don't print school names my @options = (); %hash ( des => 'Form 2', # field label label => 'Label 2', # name of field field => 'field2', # size of text input field value => '10', submit => 'submit', options => \@options, if_more => 0, );

What's the right way to do it?

Thanks in anticipation :)


In reply to HTML::Template question - tmpl_if by kiat

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.