如何使用HTML标签打印以表格格式的每个CGI脚本的输出



以下是我的循环(写为perl-cgi脚本的一部分)。我希望它使用HTML表标签在我的网页中的表格中打印。我该怎么做?

for($ff=0;$ff<scalar @phi;$ff++)
{
    print $res[$ff],"---->";
    printf("%0.2f",$omg[$ff]);
   print "<br>";
   print "n";

}

我建议创建一个包含您感兴趣的数据的数据结构。

my @rows;
# Strange that you're getting the indexes from @phi
# and the data from two different arrays, @res and @omg
for (0 .. $#phi) {
  push @rows, {
    res => $res[$_],
    omg => sprintf('%0.2f', $omg[$_]),
  };
}

然后使用模板工具包处理该数据。

use Template;
my $tt = Template->new;
$tt->process('page.tt', { rows => %rows });

page.tt中,您会有类似的东西:

<html>
  <head>
    <title>Data Page</title>
  </head>
  <body>
    <h1>Data Page</h1>
    <table>
[% FOR row IN rows -%]
      <tr><td>[% rows.res %]</td><td>[% rows.omg %]</td></tr>
[% END -%]
    </table>
  </body>
</html>

将这样的演示分开使更改数据的介绍方式变得更加容易。您甚至可以将模板提供给前端设计师,以使其看起来更好 - 他们不需要知道Perl。

最新更新