kiat has asked for the wisdom of the Perl Monks concerning the following question:
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 first template output (the one that prints the list of schools in @options) works fine# 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; }
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 :)
|
---|
Back to
Seekers of Perl Wisdom