From 47fc628b0553c6b1bf7597f29bd2b6b5ce238cbf Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 19 Oct 2012 10:42:20 -0500 Subject: simplify the logic for template suffixes now, anything that doesn't contain a '.' will have the suffix appended unconditionally, rather than looking for both the suffixed and unsuffixed versions. this is more in line with how text::xslate typically works. --- lib/Text/Handlebars/Compiler.pm | 50 +++++++++--------------------------- lib/Text/Xslate/Syntax/Handlebars.pm | 2 +- t/mustache-spec.t | 10 +++++++- t/partials.t | 4 +-- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/lib/Text/Handlebars/Compiler.pm b/lib/Text/Handlebars/Compiler.pm index 4681d30..356baeb 100644 --- a/lib/Text/Handlebars/Compiler.pm +++ b/lib/Text/Handlebars/Compiler.pm @@ -77,21 +77,24 @@ sub _generate_partial { my $self = shift; my ($node) = @_; - my $lvar_id = $self->lvar_id; - local $self->{lvar_id} = $self->lvar_use(1); - my $lvar = $node->clone(arity => 'lvar', id => $lvar_id); + my $file = $node->first; + if (ref($file) eq 'ARRAY') { + $file = $node->clone( + arity => 'binary', + id => '~', + first => $file->[0], + second => $self->call($node, '(suffix)'), + ); + } return ( - $self->compile_ast( - $self->save_lvar($lvar_id, $self->find_file($node->first->clone)), - ), $self->compile_ast( $self->make_ternary( - $lvar->clone, + $self->find_file($file->clone), $node->clone( arity => 'include', id => 'include', - first => $lvar->clone, + first => $file->clone, ), $self->literal(''), ), @@ -103,36 +106,7 @@ sub find_file { my $self = shift; my ($filename) = @_; - return $filename->clone( - arity => 'find_file', - first => $filename, - ); -} - -sub _generate_find_file { - my $self = shift; - my ($node) = @_; - - my $filename = $node->first; - my $with_suffix = $self->parser->symbol('~')->clone( - arity => 'binary', - first => $filename->clone, - second => $self->call($node, '(suffix)'), - ); - - return ( - $self->compile_ast( - $self->make_ternary( - $self->call($node, '(find_file)', $filename->clone), - $filename->clone, - $self->make_ternary( - $self->call($node, '(find_file)', $with_suffix->clone), - $with_suffix->clone, - $self->literal(''), - ), - ), - ), - ); + return $self->call($filename, '(find_file)', $filename); } sub _generate_for { diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm index 957dc17..b8854ea 100644 --- a/lib/Text/Xslate/Syntax/Handlebars.pm +++ b/lib/Text/Xslate/Syntax/Handlebars.pm @@ -438,7 +438,7 @@ sub std_partial { return $symbol->clone( arity => 'partial', - first => $partial, + first => ($partial->id =~ /\./ ? $partial : [ $partial ]), ); } diff --git a/t/mustache-spec.t b/t/mustache-spec.t index 88f8560..ae69af5 100644 --- a/t/mustache-spec.t +++ b/t/mustache-spec.t @@ -18,7 +18,15 @@ for my $file (dir('t', 'mustache-spec', 'specs')->children) { && $test->{name} !~ /line endings/i; render_ok( - ($test->{partials} ? ({ path => [$test->{partials}] }) : ()), + ($test->{partials} + ? ({ + suffix => '.mustache', + path => [ + map { +{ "$_.mustache" => $test->{partials}{$_} } } + keys %{ $test->{partials} } + ] + }) + : ()), $test->{template}, fix_data($test->{data}), $test->{expected}, diff --git a/t/partials.t b/t/partials.t index 157fab6..ad7f7d9 100644 --- a/t/partials.t +++ b/t/partials.t @@ -8,7 +8,7 @@ use Test::Handlebars; { local $TODO = "unimplemented"; render_ok( { - path => [ { dude => '{{#this}}{{name}} ({{url}}) {{/this}}' } ], + path => [ { 'dude.tx' => '{{#this}}{{name}} ({{url}}) {{/this}}' } ], }, 'Dudes: {{>dude dudes}}', { @@ -24,7 +24,7 @@ render_ok( render_ok( { - path => [ { dude => '{{name}}' } ], + path => [ { 'dude.tx' => '{{name}}' } ], }, 'Dudes: {{> [dude]}}', { -- cgit v1.2.3