Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix Mojo::Template support for parentheses in expressions
  • Loading branch information
kraih committed Feb 25, 2015
1 parent bf2e175 commit da15213
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -42,6 +42,7 @@
- Added names method to Mojo::Parameters.
- Added failed and passed methods to Mojolicious::Validator::Validation.
- Added -I and -M options to prefork command.
- Fixed Mojo::Template support for parentheses in expressions. (jberger, sri)

5.82 2015-02-22
- Deprecated Mojo::Reactor::is_readable.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Template.pm
Expand Up @@ -59,11 +59,11 @@ sub build {

# Escaped
if (!$multi && ($op eq 'escp' && !$escape || $op eq 'expr' && $escape)) {
$blocks[-1] .= "\$_O .= _escape scalar $value";
$blocks[-1] .= "\$_O .= _escape scalar + $value";
}

# Raw
elsif (!$multi) { $blocks[-1] .= "\$_O .= scalar $value" }
elsif (!$multi) { $blocks[-1] .= "\$_O .= scalar + $value" }

# Multiline
$multi = !$next || $next->[0] ne 'text';
Expand Down Expand Up @@ -271,8 +271,8 @@ sub _wrap {

# Wrap lines
my $num = () = $code =~ /\n/g;
my $head = $self->_line(1);
$head .= "\npackage @{[$self->namespace]}; use Mojo::Base -strict;";
my $head = $self->_line(1) . "\npackage @{[$self->namespace]};";
$head .= " use Mojo::Base -strict; no warnings 'ambiguous';";
$code = "$head sub { my \$_O = ''; @{[$self->prepend]}; { $code\n";
$code .= $self->_line($num + 1) . "\n@{[$self->append]}; } \$_O };";

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -108,7 +108,7 @@ Perl code, a naive translation could look like this.
$output .= '<ul>';
for my $j (1 .. $i) {
$output .= '<li>';
$output .= xml_escape scalar $j;
$output .= xml_escape scalar + $j;
$output .= '</li>';
}
$output .= '</ul>';
Expand Down Expand Up @@ -661,12 +661,12 @@ A naive translation of the template to Perl code could look like this.
my $name = shift;
my $output = '';
$output .= 'Hello ';
$output .= xml_escape scalar $name;
$output .= xml_escape scalar + $name;
$output .= '.';
return Mojo::ByteStream->new($output);
};
$output .= xml_escape scalar $block->('Sebastian');
$output .= xml_escape scalar $block->('Sara');
$output .= xml_escape scalar + $block->('Sebastian');
$output .= xml_escape scalar + $block->('Sara');
return $output;

While template blocks cannot be shared between templates, they are most
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -20,8 +20,9 @@ sub render {
my ($self, $content, $file, $conf, $app) = @_;

# Application instance and helper
my $prepend = q[my $app = shift; no strict 'refs'; no warnings 'redefine';];
$prepend .= q[sub app; local *app = sub { $app }; use Mojo::Base -strict;];
my $prepend = q[no strict 'refs'; no warnings 'redefine';];
$prepend .= q[my $app = shift; sub app; local *app = sub { $app };];
$prepend .= q[use Mojo::Base -strict; no warnings 'ambiguous';];

my $mt = Mojo::Template->new($conf->{template} || {})->name($file);
my $output = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
Expand Down
5 changes: 5 additions & 0 deletions t/mojo/template.t
Expand Up @@ -30,6 +30,11 @@ $mt->prepend('my @foo = (3, 4);')->parse('<%= @foo %>:<%== @foo %>');
my $output = $mt->build->compile || $mt->interpret;
is $output, "2:2\n", 'same context';

# Parentheses
$mt = Mojo::Template->new;
$output = $mt->render('<%= (1,2,3)[1] %><%== (1,2,3)[2] %>');
is $output, "23\n", 'no ambiguity';

# Trim tag
$mt = Mojo::Template->new;
$output = $mt->render(" ♥ <%= 'test♥' =%> \n");
Expand Down

0 comments on commit da15213

Please sign in to comment.