简体   繁体   English

如何在Perl中退出tmpl循环?

[英]How to exit a tmpl loop in perl?

I use html templates in Perl to generate a dynamic website, my Script1.pm generates a table and sends it as tmpl_loop to the template.tmpl to show a table on the website. 我在Perl中使用html模板生成一个动态网站,我的Script1.pm生成一个表并将其作为tmpl_loop发送到template.tmpl以在网站上显示一个表。

This works pretty well so far, but as the table gets bigger than about 100,000 rows, the whole browser starts to lag. 到目前为止,该方法运行良好,但是随着表的行数超过100,000行,整个浏览器开始滞后。

Can I somehow set a counter to exit the tmpl loop after 10000 iterations? 我可以设置计数器以在10000次迭代后退出tmpl循环吗?

Just making the table smaller in the script doesn't work cause I need it completed for an export file. 仅在脚本中缩小表是行不通的,因为我需要为导出文件完成该表。

If you're using Template::Toolkit , you can use the special loop variable: 如果您使用Template :: Toolkit ,则可以使用特殊的loop变量:

[% FOREACH match IN results %]
    [% LAST IF loop.count > 10000 %]
[% END %]

Note that BREAK is also an alias for LAST . 请注意, BREAK也是LAST的别名。

Trim the data before sending it to the template in the first place. 首先整理数据,然后再将其发送到模板。

For example, change 例如改变

 $template->param(ROWS => \@rows);

to

 splice(@rows, 10_000);
 $template->param(ROWS => \@rows);

or 要么

 my @truncated_rows = @rows[0 .. $#rows < 10_000 ? $#rows : 10_000];
 $template->param(ROWS => \@truncated_rows);

The latter is non-destructive, so if you need the entire set of rows for another purpose later in the program, they will still be available. 后者是非破坏性的,因此,如果以后在程序中需要整个行集以用于其他目的,则它们仍然可用。 (It seems fishy to continue processing after the output is rendered, but an update to the question seems to indicate this is a requirement.) (呈现输出后继续处理似乎很麻烦,但是对该问题的更新似乎表明这是必需的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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