如何在Perl的Template Toolkit中仅渲染特定的“BLOCK”?

前端之家收集整理的这篇文章主要介绍了如何在Perl的Template Toolkit中仅渲染特定的“BLOCK”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何只在模板中渲染特定的BLOCK?

假设我在text.tt,一个Template Toolkit文件中有这个BLOCK:

[% 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' } );

test.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相关文章