How can I only display a specific “BLOCK” in the Perl template toolbox?

How can I display only the specific BLOCKin the template?

Suppose I have this BLOCKin text.tt, Template Toolkit file :

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

I want to be able to use process()only this part to process:

$tt->process("text.tt/someblock", {...}, {...});

Is this the right way to handle this?

+3
source share
1 answer

I think this is a variant of EXPOSE_BLOCKS, which you may be after?

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 %]

 

Will produce this report (with random numbers):

Weekly report

  • Results for Mon - 728

  • Results for W are 363

  • Results for Weds - 772

  • 864

  • Fri 490

  • Sat - 88

  • - 887

, 1 1999 .

 

, .

+7

Source: https://habr.com/ru/post/1715226/


All Articles