diff options
-rw-r--r-- | lib/Text/Xslate/Syntax/Handlebars.pm | 4 | ||||
-rw-r--r-- | t/block-helpers.t | 94 | ||||
-rw-r--r-- | t/blocks.t | 45 |
3 files changed, 143 insertions, 0 deletions
diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm index ee4639c..c08cee9 100644 --- a/lib/Text/Xslate/Syntax/Handlebars.pm +++ b/lib/Text/Xslate/Syntax/Handlebars.pm @@ -64,6 +64,10 @@ sub split_tags { $input =~ s/\A\Q$close_tag// or die "Oops!"; + # XXX this is ugly, but i don't know how to get the parsing + # right otherwise if we also need to support ^foo + $code = 'else' if $code eq '^'; + my @extra; my $autochomp = $code =~ m{^[!#^/=>]} || $code eq 'else'; diff --git a/t/block-helpers.t b/t/block-helpers.t index 961b2dc..ac6f54f 100644 --- a/t/block-helpers.t +++ b/t/block-helpers.t @@ -375,4 +375,98 @@ RENDERED ); } +render_ok( + { + function => { + list => sub { + my ($context, $items, $options) = @_; + + if (@$items) { + my $out = '<ul>'; + for my $item (@$items) { + $out .= '<li>'; + $out .= $options->{fn}->($item); + $out .= '</li>'; + } + $out .= '</ul>'; + return $out; + } + else { + return '<p>' . $options->{inverse}->($context) . '</p>'; + } + }, + }, + }, + q[{{#list people}}{{name}}{{^}}<em>Nobody's here</em>{{/list}}], + { + people => [ + { name => 'Alan' }, + { name => 'Yehuda' }, + ], + }, + q[<ul><li>Alan</li><li>Yehuda</li></ul>], + "helper with inverse" +); + +render_ok( + { + function => { + list => sub { + my ($context, $items, $options) = @_; + + if (@$items) { + my $out = '<ul>'; + for my $item (@$items) { + $out .= '<li>'; + $out .= $options->{fn}->($item); + $out .= '</li>'; + } + $out .= '</ul>'; + return $out; + } + else { + return '<p>' . $options->{inverse}->($context) . '</p>'; + } + }, + }, + }, + q[{{#list people}}{{name}}{{^}}<em>Nobody's here</em>{{/list}}], + { + people => [], + }, + q[<p><em>Nobody's here</em></p>], + "helper with inverse (empty)" +); + +render_ok( + { + function => { + list => sub { + my ($context, $items, $options) = @_; + + if (@$items) { + my $out = '<ul>'; + for my $item (@$items) { + $out .= '<li>'; + $out .= $options->{fn}->($item); + $out .= '</li>'; + } + $out .= '</ul>'; + return $out; + } + else { + return '<p>' . $options->{inverse}->($context) . '</p>'; + } + }, + }, + }, + q[{{#list people}}Hello{{^}}{{message}}{{/list}}], + { + people => [], + message => "Nobody's here", + }, + q[<p>Nobody's here</p>], + "helper with inverse (inverse has variables)" +); + done_testing; @@ -70,4 +70,49 @@ render_ok( "empty block" ); +{ local $TODO = "unimplemented"; +render_ok( + '{{#people}}{{name}}{{^}}{{none}}{{/people}}', + { + none => 'No people', + }, + 'No people', + "inverted block shorthand" +); + +render_ok( + '{{#people}}{{name}}{{^}}{{none}}{{/people}}', + { + none => 'No people', + people => [], + }, + 'No people', + "inverted block shorthand (empty array)" +); + +render_ok( + <<'TEMPLATE', +{{#people}} +{{name}} +{{^}} +{{none}} +{{/people}} +TEMPLATE + { + none => 'No people', + people => [ + 'Jesse Luehrs', + 'Shawn Moore', + 'Stevan Little', + ], + }, + <<'RENDERED', +Jesse Luehrs +Shawn Moore +Stevan Little +RENDERED + "inverted block shorthand (non-empty array)" +); +} + done_testing; |