perl - 如何在 Perl 的模板工具包中仅呈现特定的 `BLOCK`?

标签 perl template-toolkit

如何仅渲染特定的 BLOCK在模板中?

假设我有这个 BLOCKtext.tt , Template Toolkit文件:

[% BLOCK someblock %] some block test blah blah blah [% END %]

我希望能够使用 process()只处理那部分:
$tt->process("text.tt/someblock", {...}, {...});

这是处理这个问题的正确方法吗?

最佳答案

我认为它是您可能追求的 EXPOSE_BLOCKS 选项?

use strict;
use warnings;
use Template;

my $tt = Template->new({
    INCLUDE_PATH  => '.',
    EXPOSE_BLOCKS => 1,
});

$tt->process( 'test.tt/header', { tit => 'Weekly report' } );

for my $day qw(Mon Tues Weds Thurs Fri Sat Sun) {
    $tt->process( 'test.tt/body', { day => $day, result => int rand 999 } );
}

$tt->process( 'test.tt/footer', { tit => '1st Jan 1999' } );

测试.tt:
[% BLOCK header %]
[% tit %]
[% END %]

[% BLOCK body %]
* Results for [% day %] are [% result %]
[% END %]

[% BLOCK footer %]
Correct for week commencing [% tit %]
[% END %]

将生成此报告(带有随机数):

Weekly report

  • Results for Mon are 728

  • Results for Tues are 363

  • Results for Weds are 772

  • Results for Thurs are 864

  • Results for Fri are 490

  • Results for Sat are 88

  • Results for Sun are 887

Correct for week commencing 1st Jan 1999



希望有帮助。

关于perl - 如何在 Perl 的模板工具包中仅呈现特定的 `BLOCK`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281391/

相关文章:

Perl 需要明确的包名

regex - Perl正则表达式如何只转义一些字符

perl - 如何通过调用外部程序来替换 perl 模块的使用?

html - 模板工具包 FOR 和 IF 行为

perl - 如何使用 RDBO 在 Template Toolkit 中强制列出上下文?

perl - 如何在 Template::Toolkits CATCH block 中抛出警告?

html - 如何使用包装器将 CSS 文件应用于我的 Catalyst webapp 中的特定模板

linux - git checkout --patch,但失败,无法在@INC 中找到 Git.pm

perl - 在perl中为 "false"变量赋值的最佳方法是什么