summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorShawn M Moore <code@sartak.org>2012-09-25 17:20:21 -0400
committerShawn M Moore <code@sartak.org>2012-09-25 17:20:21 -0400
commit3b27f46a2bd3ce3d2a5a1b3f13b3c68f6a41e9ec (patch)
treea16691eb9e06a012fd1c3b588f4a32d222e32f79 /t
parent16728b70b71e5cb06d636aba06d3188f2180b86c (diff)
downloadtext-handlebars-3b27f46a2bd3ce3d2a5a1b3f13b3c68f6a41e9ec.tar.gz
text-handlebars-3b27f46a2bd3ce3d2a5a1b3f13b3c68f6a41e9ec.zip
Custom helper blocks
Diffstat (limited to 't')
-rw-r--r--t/004-helpers.t45
1 files changed, 45 insertions, 0 deletions
diff --git a/t/004-helpers.t b/t/004-helpers.t
new file mode 100644
index 0000000..3d39755
--- /dev/null
+++ b/t/004-helpers.t
@@ -0,0 +1,45 @@
+use strict;
+use warnings;
+use Test::More;
+use Text::Xslate;
+
+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
+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>',
+);
+