p5-Text-Xslate icon indicating copy to clipboard operation
p5-Text-Xslate copied to clipboard

Wishlist (TTerse): add support for multiple function/filter parameters

Open chocolateboy opened this issue 12 years ago • 0 comments

Perl: v5.16.0 Text::Xslate: 3.2.3

Multiple function/filter parameters work in Template Toolkit e.g.:

[% "The  cat  sat  on  the  mat" | replace('\s+', '_') %]

- but don't appear to be supported in Text::Xslate.

Test case (I know replace is available elsewhere: it's just an example):

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More tests => 4;
use Text::Xslate;

use constant {
    FILTER   => '[% name | replace("ohn", "ane") %]',
    FUNCTION => '[% replace(name, "ane", "ohn") %]',
};

sub replace($;$$) {
    my $string = shift;
    my $params = is(scalar(@_), 2);

    if ($params) {
        my ($search, $replace) = @_;
        $string =~ s{$search}{$replace};
    }

    return $string;
}

my $template = Text::Xslate->new(syntax => 'TTerse', function => { replace => \&replace });
my $rendered_filter = $template->render_string(FILTER, { name => 'John Doe' });
my $rendered_function = $template->render_string(FUNCTION, { name => 'Jane Doe' });

is $rendered_filter, 'Jane Doe';
is $rendered_function, 'John Doe';

chocolateboy avatar Apr 25 '14 11:04 chocolateboy