Perl模块HTML :: Template可以使用< TMPL_VAR NAME = ...>之外的其他语法吗? [英] Can the Perl Module HTML::Template use other syntaxes besides <TMPL_VAR NAME=...>?

查看:109
本文介绍了Perl模块HTML :: Template可以使用< TMPL_VAR NAME = ...>之外的其他语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Perl 模块 HTML :: Template ,并根据文档显示您可以使用 HTML 注释,而不是对其标记的大于/小于,但它对我不起作用.

从Perldoc中获取HTML :: Template

如果您对有效的HTML狂热,并且希望您的模板符合有效的标准HTML语法,您可以选择以HTML注释的形式键入模板标签.这对于希望验证其模板的HTML作者有用.HTML :: Template处理之前的HTML语法,或使用精通DTD的编辑工具的人.

 <!-TMPL_VAR NAME = PARAM1-> 

当我尝试此操作时,我的apache日志中会收到以下消息:

[2012年7月3日星期二19:24:23] [错误] [客户端:: 1] HTML :: Template:尝试设置不存在的参数'fname'-此参数名称与任何声明都不匹配在模板文件中:(die_on_bad_params => 1)在/var/www/cgi-bin/form.cgi第90行,引荐来源:.... getcontactinfo.html

设置选项 die_on_bad_params =>0 HTML :: Template-> new 方法似乎允许使用模板名称的注释格式,有人可以确认这是完成此操作的正确方法吗?/p>

编辑#1

以下是一些实际代码:

从我的.html模板文件中

 < tbody>< tr>< td>名字:</td>< td>< ;!-TMPL_VAR NAME = FNAME-></td></tr>< tr>< td>名称:</td>< td>< ;!-TMPL_VAR NAME = NAME-></td></tr>< tr class ="alt">< td>电子邮件:</td>< td>< ;!-TMPL_VAR NAME = EMAIL-></td></tr>< tr>< td>隶属关系:</td>< td>< ;!-TMPL_VAR NAME = AFFILIATION-></td></tr> 

通过我的.cgi脚本

  my $ template = HTML :: Template-> new(文件名=>'/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); 

解决方案

到目前为止,我发现的仅有的两种方法是:

1-HTML :: Template具有来自perldocs的名为vanguard_compatibility_mode ...的开关

vanguard_compatibility_mode-如果设置为1,模块将期望看到除了标准语法外,还看起来像%NAME%.还设置die_on_bad_params => 0.如果您不在Vanguard Media尝试使用旧格式模板,请不要担心这个.默认为0.

2-HTML :: Template还支持将模板标​​签嵌入注释块中,以使您的代码符合HTML,例如:<!-TMPL_NAME NAME = FNAME->

再次从perldocs中获得:

如果您对有效的HTML狂热,并且希望您的模板符合有效的标准HTML语法,您可以选择以HTML注释的形式键入模板标签.这可能对想要验证模板的HTML语法的HTML作者有用在HTML :: Template处理之前,或使用精通DTD的编辑工具.

<!-TMPL_VAR NAME = PARAM1->

在我设置 die_on_bad_params =>之前,第二个选项最初不适用于我.0 (用于构造函数).

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.

这篇关于Perl模块HTML :: Template可以使用&lt; TMPL_VAR NAME = ...&gt;之外的其他语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆