简体   繁体   中英

Can the Perl Module HTML::Template use other syntaxes besides <TMPL_VAR NAME=…>?

I'm trying to make use of the Perl module HTML::Template and according to the docs it says you can use HTML comments instead of greater-than/less-thans around its markup but it isn't working for me.

From the perldoc for HTML::Template

If you're a fanatic about valid HTML and would like your templates to conform to valid HTML syntax, you may optionally type template tags in the form of HTML comments. This may be of use to HTML authors who would like to validate their templates' HTML syntax prior to HTML::Template processing, or who use DTD-savvy editing tools.

  <!-- TMPL_VAR NAME=PARAM1 --> 

When I try this I get these messages in my apache logs:

[Tue Jul 03 19:24:23 2012] [error] [client ::1] HTML::Template : Attempt to set nonexistent parameter 'fname' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at /var/www/cgi-bin/form.cgi line 90, referer: .... getcontactinfo.html

Setting the option die_on_bad_params => 0 to the HTML::Template->new method seems to allow the comment format of the template names to work, can anyone confirm that this is the correct way to accomplish this?

EDIT #1

Here is some of the actual code:

From my .html template file

<tbody>
<tr>             <td>First Name:         </td>  <td><!-- TMPL_VAR NAME=FNAME -->           </td> </tr>
<tr>             <td>Name:               </td>  <td><!-- TMPL_VAR NAME=NAME -->           </td> </tr>
<tr class="alt"> <td>Email:              </td>  <td><!-- TMPL_VAR NAME=EMAIL -->          </td> </tr>
<tr>             <td>Affiliation:        </td>  <td><!-- TMPL_VAR NAME=AFFILIATION -->    </td> </tr>

From my .cgi script

my $template = HTML::Template->new(filename => '/var/www/html/acknowledge.html', die_on_bad_params => 0);
$template->param(FNAME          => $firstName);
$template->param(NAME           => $firstName . " " . $lastName);
$template->param(EMAIL          => $email);
$template->param(AFFILIATION    => $affiliation);

The only 2 methods I've found thus far are the following:

1 - HTML::Template has a switch called vanguard_compatibility_mode...from the perldocs

vanguard_compatibility_mode - if set to 1 the module will expect to see s that look like %NAME% in addition to the standard syntax. Also sets die_on_bad_params => 0. If you're not at Vanguard Media trying to use an old format template don't worry about this one. Defaults to 0.

2 - HTML::Template also supports embedding the template tags in comment blocks so that your code is HTML compliant, like so: <!-- TMPL_NAME NAME=FNAME -->

Again from the perldocs:

If you're a fanatic about valid HTML and would like your templates to conform to valid HTML syntax, you may optionally type template tags in the form of HTML comments. This may be of use to HTML authors who would like to validate their templates' HTML syntax prior to HTML::Template processing, or who use DTD-savvy editing tools.

<!-- TMPL_VAR NAME=PARAM1 -->

This 2nd option didn't work originally for me until I set the die_on_bad_params => 0 for the constructor.

I don't see a paramater named "fname" anywhere in your code, so I don't understand the error message. Maybe the code you posted is not the complete code?

Regarding die_on_bad_params: Yes, setting it to 0 is the way to solve this, and I personally always set it to 0 and have never understood why one would need this option anyway. In my HTML::Template::Compiled (similar alternative to HTML::Template) module I have never implemented it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM