-
-
Save anonymous/204f902508d2e66e7c2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojo/Template.pm b/lib/Mojo/Template.pm | |
index 31cca16..5cd2950 100644 | |
--- a/lib/Mojo/Template.pm | |
+++ b/lib/Mojo/Template.pm | |
@@ -13,6 +13,7 @@ has [qw(auto_escape compiled)]; | |
has capture_end => 'end'; | |
has capture_start => 'begin'; | |
has comment_mark => '#'; | |
+has context => sub { {} }; | |
has encoding => 'UTF-8'; | |
has escape => sub { \&Mojo::Util::xml_escape }; | |
has [qw(escape_mark expression_mark trim_mark)] => '='; | |
@@ -108,7 +109,8 @@ sub interpret { | |
return undef unless my $compiled = $self->compiled; | |
my $output; | |
- return eval { $output = $compiled->(@_); 1 } ? $output : $@; | |
+ my $context = delete $self->{context} || {}; | |
+ return eval { $output = $compiled->($context, @_); 1 } ? $output : $@; | |
} | |
sub parse { | |
@@ -272,8 +274,11 @@ sub _wrap { | |
my $num = () = $code =~ /\n/g; | |
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 };"; | |
+ $head .= "sub { my \$_O = ''; my \$_V = shift;"; | |
+ my $vars = join '', | |
+ map {"my \$$_ = \$_V->{'$_'};"} grep {/^\w+$/} keys %{$self->context}; | |
+ $code = "$head @{[$self->prepend]}; { $vars { $code\n"; | |
+ $code .= $self->_line($num + 1) . "\n;}@{[$self->append]}; } \$_O };"; | |
warn "-- Code for @{[$self->name]}\n@{[encode 'UTF-8', $code]}\n\n" if DEBUG; | |
return $code; | |
diff --git a/lib/Mojolicious/Plugin/EPRenderer.pm b/lib/Mojolicious/Plugin/EPRenderer.pm | |
index 639ff45..dfd20fb 100644 | |
--- a/lib/Mojolicious/Plugin/EPRenderer.pm | |
+++ b/lib/Mojolicious/Plugin/EPRenderer.pm | |
@@ -32,19 +32,15 @@ sub register { | |
++$self->{helpers} and _helpers($ns, $renderer->helpers) | |
unless $self->{helpers}; | |
- # Stash values (every time) | |
- my $prepend = 'my $self = my $c = shift; my $_S = $c->stash; {'; | |
- $prepend .= "my \$$_ = \$_S->{'$_'};" | |
- for grep {/^\w+$/} keys %{$c->stash}; | |
- $mt->prepend($prepend . $mt->prepend)->append(';}' . $mt->append); | |
- | |
- $cache->set($key => $mt); | |
+ $cache->set( | |
+ $key => $mt->prepend('my $self = my $c = shift;' . $mt->prepend)); | |
} | |
- # Make current controller available | |
+ # Make current controller and stash available | |
no strict 'refs'; | |
no warnings 'redefine'; | |
local *{"${ns}::_C"} = sub {$c}; | |
+ $options->{'mojo.template'}->context($c->stash); | |
# Render with "epl" handler | |
$renderer->handlers->{epl}($renderer, $c, $output, $options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment