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 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'lib') 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; } -- cgit v1.2.3-54-g00ecf