From 17d50c83f2a4be7405fa151fb66620c132b01f0f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Oct 2012 18:21:30 -0500 Subject: implement the built-in block helpers --- lib/Text/Handlebars.pm | 31 ++++++++ t/block-helper-builtins.t | 178 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 t/block-helper-builtins.t diff --git a/lib/Text/Handlebars.pm b/lib/Text/Handlebars.pm index 11de467..71b1ac1 100644 --- a/lib/Text/Handlebars.pm +++ b/lib/Text/Handlebars.pm @@ -64,6 +64,37 @@ sub options { my $options = $class->SUPER::options(@_); $options->{compiler} = 'Text::Handlebars::Compiler'; + $options->{function} = { + ($options->{function} ? %{ $options->{function} } : ()), + with => sub { + my ($context, $new_context, $options) = @_; + return $options->{fn}->($new_context); + }, + each => sub { + my ($context, $list, $options) = @_; + + my $ret = ''; + for my $new_context (@$list) { + $ret .= $options->{fn}->($new_context); + } + + return $ret; + }, + if => sub { + my ($context, $conditional, $options) = @_; + if ($conditional) { + return $options->{fn}->($context); + } + return ''; + }, + unless => sub { + my ($context, $conditional, $options) = @_; + unless ($conditional) { + return $options->{fn}->($context); + } + return ''; + }, + }, return $options; } diff --git a/t/block-helper-builtins.t b/t/block-helper-builtins.t new file mode 100644 index 0000000..338e067 --- /dev/null +++ b/t/block-helper-builtins.t @@ -0,0 +1,178 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use lib 't/lib'; +use Test::More; +use Test::Handlebars; + +render_ok( + <<'TEMPLATE', +
+

{{title}}

+ + {{#with author}} +

By {{firstName}} {{lastName}}

+ {{/with}} +
+TEMPLATE + { + title => 'My first post!', + author => { + firstName => 'Charles', + lastName => 'Jolley', + }, + }, + <<'RENDERED', +
+

My first post!

+ +

By Charles Jolley

+
+RENDERED + "with helper" +); + +{ local $TODO = "unimplemented"; local $SIG{__WARN__} = sub { }; +render_ok( + <<'TEMPLATE', + +TEMPLATE + { + people => [ + "Yehuda Katz", + "Alan Johnson", + "Charles Jolley", + ], + }, + <<'RENDERED', + +RENDERED + "each helper" +); +} + +render_ok( + <<'TEMPLATE', +
+ {{#if author}} +

{{firstName}} {{lastName}}

+ {{/if}} +
+TEMPLATE + {}, + <<'RENDERED', +
+
+RENDERED + "if helper (false)" +); + +render_ok( + <<'TEMPLATE', +
+ {{#if author}} +

{{firstName}} {{lastName}}

+ {{/if}} +
+TEMPLATE + { + author => 1, + firstName => "Yehuda", + lastName => "Katz", + }, + <<'RENDERED', +
+

Yehuda Katz

+
+RENDERED + "if helper (true)" +); + +{ local $TODO = "unimplemented"; +render_ok( + <<'TEMPLATE', +
+ {{#if author}} +

{{firstName}} {{lastName}}

+ {{else}} +

Unknown Author

+ {{/if}} +
+TEMPLATE + {}, + <<'RENDERED', +
+

Unknown Author

+
+RENDERED + "if/else helper (false)" +); + +render_ok( + <<'TEMPLATE', +
+ {{#if author}} +

{{firstName}} {{lastName}}

+ {{else}} +

Unknown Author

+ {{/if}} +
+TEMPLATE + { + author => 1, + firstName => "Yehuda", + lastName => "Katz", + }, + <<'RENDERED', +
+

Yehuda Katz

+
+RENDERED + "if/else helper (true)" +); +} + +render_ok( + <<'TEMPLATE', +
+ {{#unless license}} +

WARNING: This entry does not have a license!

+ {{/unless}} +
+TEMPLATE + {}, + <<'RENDERED', +
+

WARNING: This entry does not have a license!

+
+RENDERED + "unless helper (false)" +); + +render_ok( + <<'TEMPLATE', +
+ {{#unless license}} +

WARNING: This entry does not have a license!

+ {{/unless}} +
+TEMPLATE + { + license => 1, + }, + <<'RENDERED', +
+
+RENDERED + "unless helper (true)" +); + +done_testing; -- cgit v1.2.3-54-g00ecf