Permalink
Browse files

fixed comment on last line bug in Mojo::Template (closes #603)

  • Loading branch information...
1 parent 2870e02 commit ae158f2ae4cea6e4f819dbe478e1ccfac7d5562c @kraih committed Mar 7, 2014
Showing with 26 additions and 12 deletions.
  1. +3 −1 Changes
  2. +14 −10 lib/Mojo/Template.pm
  3. +9 −1 t/mojo/template.t
View
@@ -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.
View
@@ -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
@@ -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) = @_;
@@ -269,7 +273,7 @@ sub _trim {
}
sub _wrap {
- my ($self, $lines) = @_;
+ my ($self, $code) = @_;
# Escape function
my $escape = $self->escape;
@@ -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;
}
View
@@ -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';
@@ -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');

0 comments on commit ae158f2

Please sign in to comment.