Skip to content

Commit

Permalink
fixed comment on last line bug in Mojo::Template (closes #603)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 7, 2014
1 parent 2870e02 commit ae158f2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

4.88 2014-03-06
4.88 2014-03-07
- Improved exception page to show better context information for templates.
- Fixed comment on last line bug in Mojo::Template.

4.87 2014-03-04
- Improved Mojo::ByteStream to allow more method chaining.
Expand Down
24 changes: 14 additions & 10 deletions lib/Mojo/Template.pm
Expand Up @@ -85,17 +85,15 @@ sub build {
}
}

return $self->code($self->_wrap(\@lines))->tree([]);
return $self->code(join "\n", @lines)->tree([]);
}

sub compile {
my $self = shift;

# Compile with line directive
return undef unless my $code = $self->code;
my $name = $self->name;
$name =~ y/"//d;
my $compiled = eval qq{#line 1 "$name"\n$code};
my $compiled = eval $self->_wrap($code);
$self->compiled($compiled) and return undef unless $@;

# Use local stacktrace for compile exceptions
Expand Down Expand Up @@ -248,6 +246,12 @@ sub render_file {
return $self->render($template, @_);
}

sub _line {
my $name = shift->name;
$name =~ y/"//d;
return qq{#line @{[shift]} "$name"};
}

sub _trim {
my ($self, $line) = @_;

Expand All @@ -269,7 +273,7 @@ sub _trim {
}

sub _wrap {
my ($self, $lines) = @_;
my ($self, $code) = @_;

# Escape function
my $escape = $self->escape;
Expand All @@ -279,12 +283,12 @@ sub _wrap {
};

# Wrap lines
my $first = $lines->[0] ||= '';
$lines->[0] = "package @{[$self->namespace]}; use Mojo::Base -strict;";
$lines->[0] .= "sub { my \$_M = ''; @{[$self->prepend]}; do { $first";
$lines->[-1] .= "@{[$self->append]}; \$_M } };";
my $last = scalar split "\n", $code;
my $head = $self->_line(1);
$head .= "\npackage @{[$self->namespace]}; use Mojo::Base -strict;";
$code = "$head sub { my \$_M = ''; @{[$self->prepend]}; do { $code\n";
$code .= $self->_line($last) . "\n@{[$self->append]}; \$_M } };";

my $code = join "\n", @$lines;
warn "-- Code for @{[$self->name]}\n@{[encode 'UTF-8', $code]}\n\n" if DEBUG;
return $code;
}
Expand Down
10 changes: 9 additions & 1 deletion t/mojo/template.t
Expand Up @@ -762,7 +762,6 @@ $mt->parse(<<'EOF');
</html>
EOF
$mt->build;
like $mt->code, qr/^package /, 'right code';
like $mt->code, qr/lala/, 'right code';
unlike $mt->code, qr/ comment lalala /, 'right code';
ok !defined($mt->compiled), 'nothing compiled';
Expand Down Expand Up @@ -934,6 +933,15 @@ great!
EOF
is $output, "works!\ngreat!\n", 'comments did not affect the result';

# Inline comment on last line
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
% if (1) {
works!
% } # tset
EOF
is $output, "works!\n", 'comment did not affect the result';

# Multiline expression
$mt = Mojo::Template->new;
$output = $mt->render(<<'EOF');
Expand Down

0 comments on commit ae158f2

Please sign in to comment.