Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-20 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found