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


In reply to HTML::Template not finding parameter in template by bowei_99

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.