xslate / p5-Text-Xslate

Scalable template engine for Perl5

Home Page:https://metacpan.org/release/Text-Xslate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Call function without arguments

mrmaloof opened this issue · comments

I've defined a function, year, using the bridge but I'm unsure how to invoke it my template. The function does not take any arguments.

<: year() :> # Throws an error
<: (0).year() :> # ok, 2015

use strict;
use warnings;
use Test::More;
use Text::Xslate;

{

    package MyBridge;
    use parent qw(Text::Xslate::Bridge);
    use Time::Piece;
    __PACKAGE__->bridge(
        scalar => { year => \&year },
        array  => {},
        hash   => {},
    );
    sub year { localtime->year }
    1;
}

my $tx = Text::Xslate->new( module => [qw(MyBridge)], );

my $res = $tx->render_string('<: (0).year() :>');
is( $res, 2015 );

$res = $tx->render_string('<: year() :>');
is( $res, 2015 );

Sorry very too late reply. You should use function option of constructor in such case(Please see http://search.cpan.org/~syohex/Text-Xslate-3.3.9/lib/Text/Xslate.pm#Methods). You have to specify scalar invocant for method.

use strict;
use warnings;
use Test::More;
use Text::Xslate;
use Time::Piece;

my $tx = Text::Xslate->new(
    function => {
        year => sub { localtime->year },
    },
);

my $res = $tx->render_string('<: year() :>');
is( $res, 2016 );

done_testing;