bowei_99 has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble getting my code with HTML::Template to work; perl seems to think that it can't find the parameter, even though it parsed it. My code, slightly modified from the CPAN page:
#!/usr/bin/perl use HTML::Template; # open the html template my $template = HTML::Template->new(debug => 1, filename => 'site-page. +tmpl'); #fill in some parameters $template->param(SLOTNAME => 'test-slot-name'); $template->param(SITE => "sitename-here"); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n", $template->output;
My HTML Template:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859 +-1"> <title><TMPL_VAR NAME="SITE"> chassis/blade information</title> </head> <body> <h1><TMPL_VAR NAME="SITE"> chassis/blade information</h1> <table style="" border="1" cellpadding="2" cellspacing="2"> <tbody> <TMPL_LOOP NAME="chassis_type"> <tr> <TMPL_LOOP NAME="chassis_id"> <td style="width: 216px;"> <img style="width: 185px; height: 169px;" alt="<TMPL_VAR NAME=SLOTNAME>" src="<TMPL_VAR NAME=BASEURL><TMPL_VAR NAME=SLOTNAME>.jpg"><br> <TMPL_VAR NAME="SLOTNAME"> </td> </TMPL_LOOP> </tr> </TMPL_LOOP> </tbody> </table> <br> </body> </html>
When I run the code, I get this:
# ./test-templ.pl ### HTML::Template Debug ### In _parse: ### HTML::Template Debug ### site-page.tmpl : line 4 : parsed VAR site ### HTML::Template Debug ### site-page.tmpl : line 7 : parsed VAR site ### HTML::Template Debug ### site-page.tmpl : line 11 : LOOP chassis_t +ype start ### HTML::Template Debug ### site-page.tmpl : line 13 : LOOP chassis_i +d start ### HTML::Template Debug ### site-page.tmpl : line 16 : parsed VAR slo +tname ### HTML::Template Debug ### site-page.tmpl : line 17 : parsed VAR bas +eurl ### HTML::Template Debug ### site-page.tmpl : line 17 : parsed VAR slo +tname ### HTML::Template Debug ### site-page.tmpl : line 18 : parsed VAR slo +tname ### HTML::Template Debug ### site-page.tmpl : line 20 : LOOP end ### HTML::Template Debug ### site-page.tmpl : line 22 : LOOP end HTML::Template : Attempt to set nonexistent parameter 'slotname' - thi +s parameter name doesn't match any declarations in the template file +: (die_on_bad_params => 1) at ./test-templ.pl line 9
When I comment out the first filling in of parameters (so only the site is filled in), the script works. I'd thought it might be something with the template, but according to the CPAN page, it's fine. From http://search.cpan.org/~samtregar/HTML-Template-2.6/Template.pm:
HTML::Template's tags are meant to mimic normal HTML tags. However, th +ey are allowed to "break the rules". Something like: <img src="<TMPL_VAR IMAGE_SRC>"> is not really valid HTML, but it is a perfectly valid use and will wor +k as planned.
Also:
Q: What characters are allowed in TMPL_* NAMEs? A: Numbers, letters, '.', '/', '+', '-' and '_'.
In other words, when I have alt="<TMPL_VAR NAME=SLOTNAME>", it should be just as legal as <TMPL_VAR NAME="SITE">, since the latter pretty much matches what's in the CPAN page. Moreover, from the debug output earlier, the script *did* in fact parse it; I'm at a loss as to why it thinks it's a nonexistent parameter.

Any thoughts on why I'm getting this error?

-- Burvil

Replies are listed 'Best First'.
Re: HTML::Template not finding parameter in template
by Loops (Curate) on Nov 22, 2014 at 02:56 UTC

    When your template variable is inside a <TMPL_LOOP> you have to qualify it with the loop name:

    $template->param( chassis_type => [{ chassis_id => [{ SLOTNAME => 'test-slot-name' }] }] );

    The template doesn't demand a global SLOTNAME value, it requires a value for every iteration of the loop.