Skip to content

Commit

Permalink
just use the template options
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 7, 2016
1 parent ddc627d commit 87c085d
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -10,10 +10,13 @@ sub register {
my ($self, $app, $conf) = @_;

# Auto escape by default to prevent XSS attacks
my $template = {auto_escape => 1, %{$conf->{template} || {}}, vars => 1};
my $ns = $self->{namespace} = $template->{namespace}
my $dialect = {auto_escape => 1, %{$conf->{template} || {}}, vars => 1};
my $ns = $self->{namespace} = $dialect->{namespace}
//= 'Mojo::Template::Sandbox::' . md5_sum "$self";
# Make "$self" and "$c" available in templates
$dialect->{prepend} = 'my $self = my $c = _C;' . ($dialect->{prepend} // '');

# Add "ep" handler and make it the default
$app->renderer->default_handler('ep')->add_handler(
$conf->{name} || 'ep' => sub {
Expand All @@ -23,24 +26,18 @@ sub register {
return unless defined $name;
my $key = md5_sum encode 'UTF-8', $name;

# Prepare template for "epl" handler
my $cache = $renderer->cache;
my $mt = $cache->get($key);
unless ($mt) {
$cache->set($key => $mt = Mojo::Template->new($template));
$mt->prepend('my $self = my $c = _C;' . $mt->prepend);
$cache->set($key => $mt = Mojo::Template->new($dialect)) unless $mt;

# Helpers (only once)
++$self->{helpers} and _helpers($ns, $renderer->helpers)
unless $self->{helpers};
}
# Export helpers only once
++$self->{helpers} and _helpers($ns, $renderer->helpers)
unless $self->{helpers};

# Make current controller available
# Make current controller available and render with "epl" handler
no strict 'refs';
no warnings 'redefine';
local *{"${ns}::_C"} = sub {$c};

# Render with "epl" handler
Mojolicious::Plugin::EPLRenderer::_render($renderer, $c, $output,
$options, $mt, $c->stash);
}
Expand Down

0 comments on commit 87c085d

Please sign in to comment.