html - Perl 模块 HTML::Template 可以使用 <TMPL_VAR NAME=...> 之外的其他语法吗?

标签 html perl templates

我正在尝试使用 Perl 模块 HTML::Template 并且根据文档,它说您可以使用 HTML评论而不是大于/小于其标记,但它对我不起作用。

来自 HTML::Template 的 perldoc

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 -->

当我尝试这样做时,我在我的 Apache 日志中收到了这些消息:

[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

将选项 die_on_bad_params => 0 设置为 HTML::Template->new 方法似乎允许模板名称的注释格式起作用,任何人都可以确认这是完成此任务的正确方法吗?

编辑#1

下面是一些实际的代码:

来 self 的 .html 模板文件

<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>

来 self 的 .cgi 脚本

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);

最佳答案

到目前为止,我发现的唯一两种方法如下:

1 - HTML::Template 有一个名为 vanguard_compatibility_mode 的开关...来自 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 还支持在注释 block 中嵌入模板标签,以便您的代码符合 HTML 标准,如下所示:<!-- TMPL_NAME NAME=FNAME -->

再次来自 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 -->

第二个选项最初对我不起作用,直到我设置了 die_on_bad_params => 0对于构造函数。

关于html - Perl 模块 HTML::Template 可以使用 <TMPL_VAR NAME=...> 之外的其他语法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320725/

相关文章:

Javascript getAttribute 对图像返回 null

perl - 在 Perl 中,用户提供的格式说明符总是安全的吗?

regex - 需要过滤日志以搜索最近 5 分钟的行

python - Flask api 中的自定义静态路径?

templates - 在velocity模板中调用java方法

html - Bootstrap collapsible 在我的模板中不起作用

html - 为什么我的 CSS 动画没有播放?

javascript - MW 小部件上的内容应在第一次单击时展开,但会缩小

perl - Perl 是编译型编程语言还是解释型编程语言?

c++ - 关于模板代码组织 : where to put code that a template uses