summaryrefslogtreecommitdiffstats
path: root/t/helpers-examples.t
blob: 1df8e993ad665c26c2a28442cd7c401afe6e6678 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib';
use Test::More;
use Test::Handlebars;

use Text::Xslate 'mark_raw';

render_ok(
    {
        function => {
            fullName => sub {
                my ($context, $person) = @_;
                return $person->{firstName} . ' ' . $person->{lastName};
            },
        },
    },
    <<'TEMPLATE',
<div class="post">
  <h1>By {{fullName author}}</h1>
  <div class="body">{{body}}</div>

  <h1>Comments</h1>

  {{#each comments}}
  <h2>By {{fullName author}}</h2>
  <div class="body">{{body}}</div>
  {{/each}}
</div>
TEMPLATE
    {
        author   => { firstName => "Alan", lastName => "Johnson" },
        body     => "I Love Handlebars",
        comments => [
            {
                author => { firstName => "Yehuda", lastName => "Katz" },
                body   => "Me too!"
            },
        ],
    },
    <<'RENDERED',
<div class="post">
  <h1>By Alan Johnson</h1>
  <div class="body">I Love Handlebars</div>

  <h1>Comments</h1>

  <h2>By Yehuda Katz</h2>
  <div class="body">Me too!</div>
</div>
RENDERED
    "example"
);

render_ok(
    {
        function => {
            agree_button => sub {
                my ($context) = @_;
                return mark_raw(
                    "<button>I agree. I "
                  . $context->{emotion}
                  . ' '
                  . $context->{name}
                  . "</button>"
                );
            },
        },
    },
    <<'TEMPLATE',
<ul>
  {{#each items}}
  <li>{{agree_button}}</li>
  {{/each}}
</ul>
TEMPLATE
    {
        items => [
            { name => "Handlebars", emotion => "love" },
            { name => "Mustache",   emotion => "enjoy" },
            { name => "Ember",      emotion => "want to learn" },
        ],
    },
    <<'RENDERED',
<ul>
  <li><button>I agree. I love Handlebars</button></li>
  <li><button>I agree. I enjoy Mustache</button></li>
  <li><button>I agree. I want to learn Ember</button></li>
</ul>
RENDERED
    "example"
);

done_testing;