From 1a010552eeb2a965c92fab007c45f8e59ac60b02 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 5 Oct 2012 10:39:27 -0500 Subject: implement else blocks --- lib/Text/Handlebars.pm | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'lib/Text/Handlebars.pm') diff --git a/lib/Text/Handlebars.pm b/lib/Text/Handlebars.pm index c1afc32..cf2a8f1 100644 --- a/lib/Text/Handlebars.pm +++ b/lib/Text/Handlebars.pm @@ -85,14 +85,14 @@ sub options { if ($conditional) { return $options->{fn}->($context); } - return ''; + return $options->{inverse} ? $options->{inverse}->($context) : ''; }, unless => sub { my ($context, $conditional, $options) = @_; unless ($conditional) { return $options->{fn}->($context); } - return ''; + return $options->{inverse} ? $options->{inverse}->($context) : ''; }, }, @@ -119,14 +119,19 @@ sub _register_builtin_methods { return 0; }; $funcs->{'(run_block_helper)'} = sub { - my ($code, $raw_text, $vars, @args) = @_; + my ($code, $raw_text, $else_raw_text, $vars, @args) = @_; - my $recurse = sub { + my $options = {}; + $options->{fn} = sub { my ($new_vars) = @_; return $weakself->render_string($raw_text, $new_vars); }; + $options->{inverse} = sub { + my ($new_vars) = @_; + return $weakself->render_string($else_raw_text, $new_vars); + }; - return $code->($vars, @args, { fn => $recurse }); + return $code->($vars, @args, $options); }; } -- cgit v1.2.3-54-g00ecf