diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-10-04 18:21:30 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-10-04 18:21:30 -0500 |
commit | 17d50c83f2a4be7405fa151fb66620c132b01f0f (patch) | |
tree | b11f8bc9aa769a073ad7d6d830a808b6b3430497 /lib | |
parent | 6a0925c2c2028654fe8facb9f4388352b8d380d6 (diff) | |
download | text-handlebars-17d50c83f2a4be7405fa151fb66620c132b01f0f.tar.gz text-handlebars-17d50c83f2a4be7405fa151fb66620c132b01f0f.zip |
implement the built-in block helpers
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Text/Handlebars.pm | 31 |
1 files changed, 31 insertions, 0 deletions
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; } |