Skip to content

Instantly share code, notes, and snippets.

@jberger
Created October 26, 2012 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/3961368 to your computer and use it in GitHub Desktop.
Save jberger/3961368 to your computer and use it in GitHub Desktop.
EPRenderer with template_class option
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);
...
@jberger
Copy link
Author

jberger commented Oct 26, 2012

allows plugin EPRenderer => { name => 'eptex', template_class => 'MojoX::Template::LaTeX' };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment