perl cgi |定义列表语法

时间:2010-08-08 19:20:28

标签: perl cgi

无法通过Perl查找创建定义列表的正确语法来创建输出,如下所示:

<dt>One</dt> 
        <dd><p>Testing 1</p></dd> 
<dt>Two</dt> 
        <dd><p>Testing 2</p></dd> 
<dt>Three</dt> 
        <dd><p>Testing 3</p></dd> 
</dl> 

我似乎无法找到有关用法的任何文档。我试过$cgi->dl($cgi->dt([One,Testing1,Two,Testing2,Three,Testing3])));和其他变种,但到目前为止没有运气。到目前为止,搜索Google或perldoc并没有帮助。

2 个答案:

答案 0 :(得分:3)

print $cgi->dl(
        $cgi->dt('One'),
        $cgi->dd(
                $cgi->p('Testing 1')
        ),
        $cgi->dt('Two'),
        $cgi->dd(
                $cgi->p('Testing 2')
        ),
        $cgi->dt('Three'),
        $cgi->dd(
                $cgi->p('Testing 3')
        ));

我真的切换到Template-Toolkit而不是使用CGI.pm生成数据结构。

答案 1 :(得分:2)

David对使用Template::Toolkit的语法和建议是正确的。或者另一个模板模块。

这是一个简单的例子,它在脚本的DATA部分中从模板生成页面。

当然,当您保留单独的模板文件并重复使用它们时,真正的力量就会出现。

#!perl

use strict;
use warnings;

use Template;

my $page_data = {
    title => 'DL Demo',
    data  => [
        {   terms => ['One Term'],
            data  => ['One Definition'],
        },
        {   terms => [qw( Many Terms )],
            data  => ['One Definition'],
        },
        {   terms => ['One Term'],
            data  => [qw( Many Definitions )],
        },
    ],
};

my $tt = Template->new() or die "Ugh";

$tt->process(\*DATA, $page_data);


__DATA__

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
  <head>
    <title>[% title %]</title>
  </head>
  <body> 
    <div id="header">
      <a href="/index.html" class="logo" alt="Home Page"></a>
      <h1 class="headline">[% title %]</h1>
    </div>

    <div id="data">
      <dl>
         [% FOREACH item = data %] 
         [% FOREACH term = item.terms %] <dt> [% term %] </dt> [% END %]
         [% FOREACH defdata = item.data %] <dd> [% defdata %] </dd> [% END %]
         [% END %]
      </dl>
    </div>


  </body>
</html>

这是输出:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
  <head>
    <title>DL Demo</title>
  </head>
  <body> 
    <div id="header">
      <a href="/index.html" class="logo" alt="Home Page"></a>
      <h1 class="headline">DL Demo</h1>
    </div>

    <div id="data">
      <dl>

         <dt> One Term </dt> 
         <dd> One Definition </dd> 

         <dt> Many </dt>  <dt> Terms </dt> 
         <dd> One Definition </dd> 

         <dt> One Term </dt> 
         <dd> Many </dd>  <dd> Definitions </dd> 

      </dl>
    </div>

  </body>
</html>