Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
simplify renderers a bit
  • Loading branch information
kraih committed Aug 16, 2014
1 parent 4e2233f commit e4045b1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@

5.29 2014-08-16
- Added helpers method to Mojolicious::Controller.
- Improved performance of .ep templates slightly.
- Fixed "0" value bug in Mojolicious::Plugin::EPRenderer.

5.28 2014-08-13
- Improved performance of nested helpers and helpers in templates
Expand Down
16 changes: 5 additions & 11 deletions lib/Mojolicious/Plugin/EPLRenderer.pm
Expand Up @@ -9,17 +9,8 @@ sub register { $_[1]->renderer->add_handler(epl => \&_epl) }
sub _epl {
my ($renderer, $c, $output, $options) = @_;

# Template
my $name = $renderer->template_name($options);
my $inline = $options->{inline};
$name = md5_sum encode('UTF-8', $inline) if defined $inline;
return undef unless defined $name;

# Cached
my $key = delete $options->{cache} || $name;
my $cache = $renderer->cache;
my $mt = $cache->get($key);
$mt ||= $cache->set($key => Mojo::Template->new)->get($key);
my $mt = delete $options->{'mojo.template'} || Mojo::Template->new;
my $log = $c->app->log;
if ($mt->compiled) {
$log->debug("Rendering cached @{[$mt->name]}.");
Expand All @@ -28,6 +19,9 @@ sub _epl {

# Not cached
else {
my $inline = $options->{inline};
my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef;
return undef unless defined($name //= $renderer->template_name($options));

# Inline
if (defined $inline) {
Expand All @@ -37,7 +31,7 @@ sub _epl {

# File
else {
$mt->encoding($renderer->encoding) if $renderer->encoding;
if (my $encoding = $renderer->encoding) { $mt->encoding($encoding) }

# Try template
if (defined(my $path = $renderer->template_path($options))) {
Expand Down
30 changes: 12 additions & 18 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -19,22 +19,24 @@ sub register {
$conf->{name} || 'ep' => sub {
my ($renderer, $c, $output, $options) = @_;
# Generate name
my $name = $options->{inline} || $renderer->template_name($options);
my $name = $options->{inline} // $renderer->template_name($options);
return undef unless defined $name;
my @keys = sort grep {/^\w+$/} keys %{$c->stash};
my $id = encode 'UTF-8', join(',', $name, @keys);
my $key = $options->{cache} = md5_sum $id;
my $key = md5_sum encode 'UTF-8', join(',', $name, @keys);

# Cache template for "epl" handler
# Prepare template for "epl" handler
my $cache = $renderer->cache;
my $mt = $cache->get($key);
unless ($mt) {
$mt = Mojo::Template->new($template);
unless ($options->{'mojo.template'} = $cache->get($key)) {
my $mt = $options->{'mojo.template'} = Mojo::Template->new($template);

# Helpers (only once)
++$self->{helpers} and _helpers($mt->namespace, $renderer->helpers)
unless $self->{helpers};
unless ($self->{helpers} || $self->{helpers}++) {
my $helpers = $renderer->helpers;
for my $method (grep {/^\w+$/} keys %$helpers) {
my $sub = $helpers->{$method};
monkey_patch $ns, $method, sub { $ns->_C->$sub(@_) };
}
}

# Stash values (every time)
my $prepend = 'my $self = my $c = shift; my $_S = $c->stash;';
Expand All @@ -54,14 +56,6 @@ sub register {
);
}

sub _helpers {
my ($class, $helpers) = @_;
for my $method (grep {/^\w+$/} keys %$helpers) {
my $sub = $helpers->{$method};
monkey_patch $class, $method, sub { $class->_C->$sub(@_) };
}
}

1;

=encoding utf8
Expand Down
9 changes: 7 additions & 2 deletions t/mojolicious/app.t
Expand Up @@ -225,8 +225,7 @@ like $log, qr/Rendering template "syntaxerror.html.epl"\./, 'right message';
like $log, qr/Missing right curly/, 'right message';
like $log, qr/Template "exception.development.html.ep" not found\./,
'right message';
like $log, qr/Rendering cached template "exception.html.epl"\./,
'right message';
like $log, qr/Rendering template "exception.html.epl"\./, 'right message';
like $log, qr/500 Internal Server Error/, 'right message';
$t->app->log->unsubscribe(message => $cb);

Expand Down Expand Up @@ -260,10 +259,16 @@ $t->get_ok('/fun/time' => {'X-Test' => 'Hi there!'})->status_is(200)

# Foo::fun
$url = $t->ua->server->url;
$log = '';
$cb = $t->app->log->on(message => sub { $log .= pop });
$url->path('/fun/time');
$t->get_ok($url => {'X-Test' => 'Hi there!'})->status_is(200)
->header_is('X-Bender' => undef)->header_is(Server => 'Mojolicious (Perl)')
->content_is('Have fun!');
like $log,
qr!Rendering cached template "foo/fun\.html\.ep" from DATA section\.!,
'right message';
$t->app->log->unsubscribe(message => $cb);

# Foo::fun
$t->get_ok('/happy/fun/time' => {'X-Test' => 'Hi there!'})->status_is(200)
Expand Down
2 changes: 2 additions & 0 deletions t/mojolicious/lite_app.t
Expand Up @@ -480,6 +480,8 @@ is $t->app->build_controller($t->app->ua->build_tx(GET => '/foo'))->req->url,
'/foo', 'right URL';
is $t->app->build_controller->render_to_string('index', handler => 'epl'),
'Just works!', 'right result';
is $t->app->build_controller->render_to_string(inline => '0'), "0\n",
'right result';

# Unicode snowman
$t->get_ok('/☃')->status_is(200)
Expand Down

0 comments on commit e4045b1

Please sign in to comment.