Skip to content

Commit

Permalink
generate template only on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 10, 2013
1 parent 9ef3b05 commit 03f2b12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 1 addition & 19 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -156,24 +156,6 @@ sub render {
my $args = ref $_[0] ? $_[0] : {@_};
$args->{template} = $template if $template;

# Detect template name
my $stash = $self->stash;
unless ($args->{template} || $stash->{template}) {

# Normal default template
my $controller = $args->{controller} || $stash->{controller};
my $action = $args->{action} || $stash->{action};
if ($controller && $action) {
$stash->{template} = join '/',
split(/-/, Mojo::Util::decamelize($controller)), $action;
}

# Try the route name if we don't have controller and action
elsif (my $endpoint = $self->match->endpoint) {
$stash->{template} = $endpoint->name;
}
}

# Render
my $app = $self->app;
my ($output, $format) = $app->renderer->render($self, $args);
Expand All @@ -185,7 +167,7 @@ sub render {
my $headers = $self->res->body($output)->headers;
$headers->content_type($app->types->type($format) || 'text/plain')
unless $headers->content_type;
return !!$self->rendered($stash->{status});
return !!$self->rendered($self->stash->{status});
}

sub render_data { shift->render(data => @_) }
Expand Down
22 changes: 20 additions & 2 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::Cache;
use Mojo::JSON;
use Mojo::Home;
use Mojo::Loader;
use Mojo::Util qw(encode slurp);
use Mojo::Util qw(decamelize encode slurp);

has cache => sub { Mojo::Cache->new };
has classes => sub { ['main'] };
Expand Down Expand Up @@ -94,7 +94,10 @@ sub render {
}

# Template or templateless handler
else { return unless $self->_render_template($c, \$output, $options) }
else {
$options->{template} ||= $self->_generate_template($c);
return unless $self->_render_template($c, \$output, $options);
}

# Extends
my $content = $stash->{'mojo.content'} ||= {};
Expand Down Expand Up @@ -177,6 +180,21 @@ sub _extends {
return delete $stash->{extends};
}

sub _generate_template {
my ($self, $c) = @_;

# Normal default template
my $stash = $c->stash;
my $controller = $stash->{controller};
my $action = $stash->{action};
return join '/', split(/-/, decamelize($controller)), $action
if $controller && $action;

# Try the route name if we don't have controller and action
return undef unless my $endpoint = $c->match->endpoint;
return $endpoint->name;
}

sub _render_template {
my ($self, $c, $output, $options) = @_;

Expand Down

0 comments on commit 03f2b12

Please sign in to comment.