summaryrefslogtreecommitdiffstats
path: root/t/helpers.t
blob: 72863711ccc1df90f06a709418034f50e4d5e0d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use strict;
use warnings;
use Test::More;
use Text::Xslate;

plan skip_all => "unimplemented";

my $tx = Text::Xslate->new(syntax => 'Handlebars');

# XXX I'm not sure how helpers should be registered in Perl
# in JS, it's global which is crappy
# Text::Xslate->new has a "function" parameter for registering helpers
Handlebars->registerHelper(noop => sub {
    my ($context, $options) = @_;
    return $options->{fn}->($context);
});

is(
    $tx->render_string(
        '<h1>{{title}}</h1><p>{{#noop}}{{body}}{{/noop}}</p>',
        { title => 'A', body => 'the first letter' },
    ),
    '<h1>A</h1><p>the first letter</p>',
);

Handlebars->registerHelper(list => sub {
    my ($items, $options) = @_;
    my $out = "<ul>";

    for my $item (@$items) {
        $out .= "<li>" . $options->{fn}->($item) . "</li>";
    }

    return $out . "</ul>";
});

is(
    $tx->render_string(
        '{{#list people}}{{firstName}} {{lastName}}{{/list}}',
        { people => [
            { firstName => 'Jesse',  lastName => 'Luehrs' },
            { firstName => 'Shawn',  lastName => 'Moore' },
            { firstName => 'Stevan', lastName => 'Little' },
        ] },
    ),
    '<ul><li>Jesse Luehrs</li><li>Shawn Moore</li><li>Stevan Little</li></ul>',
);

done_testing;