Created
October 26, 2012 20:41
-
-
Save jberger/3961368 to your computer and use it in GitHub Desktop.
EPRenderer with template_class option
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
package Mojolicious::Plugin::EPRenderer; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
use Mojo::Template; | |
use Mojo::Util qw(encode md5_sum); | |
use Scalar::Util (); | |
sub register { | |
my ($self, $app, $conf) = @_; | |
# Auto escape by default to prevent XSS attacks | |
my $template = {auto_escape => 1, %{$conf->{template} || {}}}; | |
my $template_class = $conf->{template_class} || 'Mojo::Template'; | |
# Add "ep" handler | |
$app->renderer->add_handler( | |
$conf->{name} || 'ep' => sub { | |
my ($renderer, $c, $output, $options) = @_; | |
# Generate name | |
my $path = $options->{inline} || $renderer->template_path($options); | |
return undef unless defined $path; | |
my $id = encode 'UTF-8', join(', ', $path, sort keys %{$c->stash}); | |
my $key = $options->{cache} = md5_sum $id; | |
# Compile helpers and stash values | |
my $cache = $renderer->cache; | |
unless ($cache->get($key)) { | |
my $mt = $template_class->new($template); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
allows
plugin EPRenderer => { name => 'eptex', template_class => 'MojoX::Template::LaTeX' };