Skip to content

Instantly share code, notes, and snippets.

/render.diff Secret

Created September 11, 2014 00:56
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 anonymous/77488e9da95f583d2728 to your computer and use it in GitHub Desktop.
Save anonymous/77488e9da95f583d2728 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious.pm b/lib/Mojolicious.pm
index ad56056..ba860f9 100644
--- a/lib/Mojolicious.pm
+++ b/lib/Mojolicious.pm
@@ -157,7 +157,8 @@ sub new {
# Default to controller and application namespace
my $r = $self->routes->namespaces(["@{[ref $self]}::Controller", ref $self]);
- # Hide controller attributes/methods and "handler"
+ # Hide controller attributes/methods, "handler", "render_exception" and
+ # "render_not_found"
$r->hide(qw(app continue cookie finish flash handler helpers match on));
$r->hide(qw(param redirect_to render render_exception render_later));
$r->hide(qw(render_maybe render_not_found render_static render_to_string));
diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm
index d59a88a..f45c4e3 100644
--- a/lib/Mojolicious/Controller.pm
+++ b/lib/Mojolicious/Controller.pm
@@ -4,7 +4,6 @@ use Mojo::Base -base;
# No imports, for security reasons!
use Carp ();
use Mojo::ByteStream;
-use Mojo::Exception;
use Mojo::Transaction::HTTP;
use Mojo::URL;
use Mojo::Util;
@@ -172,14 +171,10 @@ sub render {
return !!$self->rendered($self->stash->{status});
}
-sub render_exception { _development('exception', @_) }
-
sub render_later { shift->stash('mojo.rendered' => 1) }
sub render_maybe { shift->render(@_, 'mojo.maybe' => 1) }
-sub render_not_found { _development('not_found', @_) }
-
sub render_static {
my ($self, $file) = @_;
my $app = $self->app;
@@ -386,50 +381,6 @@ sub write_chunk {
return $self->rendered;
}
-sub _development {
- my ($page, $self, $e) = @_;
-
- my $app = $self->app;
- $app->log->error($e = Mojo::Exception->new($e)) if $page eq 'exception';
-
- # Filtered stash snapshot
- my $stash = $self->stash;
- my %snapshot = map { $_ => $stash->{$_} }
- grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;
-
- # Render with fallbacks
- my $mode = $app->mode;
- my $renderer = $app->renderer;
- my $options = {
- exception => $page eq 'exception' ? $e : undef,
- format => $stash->{format} || $renderer->default_format,
- handler => undef,
- snapshot => \%snapshot,
- status => $page eq 'exception' ? 500 : 404,
- template => "$page.$mode"
- };
- my $inline = $renderer->_bundled($mode eq 'development' ? $mode : $page);
- return $self if _fallbacks($self, $options, $page, $inline);
- _fallbacks($self, {%$options, format => 'html'}, $page, $inline);
- return $self;
-}
-
-sub _fallbacks {
- my ($self, $options, $template, $inline) = @_;
-
- # Mode specific template
- return 1 if $self->render_maybe(%$options);
-
- # Normal template
- return 1 if $self->render_maybe(%$options, template => $template);
-
- # Inline template
- my $stash = $self->stash;
- return undef unless $stash->{format} eq 'html';
- delete @$stash{qw(extends layout)};
- return $self->render_maybe(%$options, inline => $inline, handler => 'ep');
-}
-
1;
=encoding utf8
@@ -695,16 +646,6 @@ additional pairs get merged into the L</"stash">.
# Render template "test.xml.*"
$c->render('test', format => 'xml');
-=head2 render_exception
-
- $c = $c->render_exception('Oops!');
- $c = $c->render_exception(Mojo::Exception->new('Oops!'));
-
-Render the exception template C<exception.$mode.$format.*> or
-C<exception.$format.*> and set the response status code to C<500>. Also sets
-the stash values C<exception> to a L<Mojo::Exception> object and C<snapshot>
-to a copy of the L</"stash"> for use in the templates.
-
=head2 render_later
$c = $c->render_later;
@@ -730,15 +671,6 @@ could be generated, takes the same arguments as L</"render"
# Render template "index_local" only if it exists
$c->render_maybe('index_local') or $c->render('index');
-=head2 render_not_found
-
- $c = $c->render_not_found;
-
-Render the not found template C<not_found.$mode.$format.*> or
-C<not_found.$format.*> and set the response status code to C<404>. Also sets
-the stash value C<snapshot> to a copy of the L</"stash"> for use in the
-templates.
-
=head2 render_static
my $bool = $c->render_static('images/logo.png');
diff --git a/lib/Mojolicious/Plugin/DefaultHelpers.pm b/lib/Mojolicious/Plugin/D
index 0d9670c..c52b1c7 100644
--- a/lib/Mojolicious/Plugin/DefaultHelpers.pm
+++ b/lib/Mojolicious/Plugin/DefaultHelpers.pm
@@ -3,6 +3,7 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::ByteStream;
use Mojo::Collection;
+use Mojo::Exception;
use Mojo::IOLoop;
use Mojo::Util qw(dumper sha1_sum steady_time);
@@ -32,10 +33,12 @@ sub register {
qw(inactivity_timeout is_fresh url_with);
$app->helper(b => sub { shift; Mojo::ByteStream->new(@_) });
$app->helper(c => sub { shift; Mojo::Collection->new(@_) });
- $app->helper(config => sub { shift->app->config(@_) });
- $app->helper(dumper => sub { shift; dumper(@_) });
- $app->helper(include => sub { shift->render_to_string(@_) });
- $app->helper(ua => sub { shift->app->ua });
+ $app->helper(config => sub { shift->app->config(@_) });
+ $app->helper(dumper => sub { shift; dumper(@_) });
+ $app->helper(include => sub { shift->render_to_string(@_) });
+ $app->helper(render_exception => sub { _development('exception', @_) });
+ $app->helper(render_not_found => sub { _development('not_found', @_) });
+ $app->helper(ua => sub { shift->app->ua });
}
sub _accepts {
@@ -82,6 +85,50 @@ sub _delay {
$delay->catch(sub { $c->render_exception(pop) and undef $tx })->wait;
}
+sub _development {
+ my ($page, $self, $e) = @_;
+
+ my $app = $self->app;
+ $app->log->error($e = Mojo::Exception->new($e)) if $page eq 'exception';
+
+ # Filtered stash snapshot
+ my $stash = $self->stash;
+ my %snapshot = map { $_ => $stash->{$_} }
+ grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;
+
+ # Render with fallbacks
+ my $mode = $app->mode;
+ my $renderer = $app->renderer;
+ my $options = {
+ exception => $page eq 'exception' ? $e : undef,
+ format => $stash->{format} || $renderer->default_format,
+ handler => undef,
+ snapshot => \%snapshot,
+ status => $page eq 'exception' ? 500 : 404,
+ template => "$page.$mode"
+ };
+ my $inline = $renderer->_bundled($mode eq 'development' ? $mode : $page);
+ return $self if _fallbacks($self, $options, $page, $inline);
+ _fallbacks($self, {%$options, format => 'html'}, $page, $inline);
+ return $self;
+}
+
+sub _fallbacks {
+ my ($self, $options, $template, $inline) = @_;
+
+ # Mode specific template
+ return 1 if $self->render_maybe(%$options);
+
+ # Normal template
+ return 1 if $self->render_maybe(%$options, template => $template);
+
+ # Inline template
+ my $stash = $self->stash;
+ return undef unless $stash->{format} eq 'html';
+ delete @$stash{qw(extends layout)};
+ return $self->render_maybe(%$options, inline => $inline, handler => 'ep');
+}
+
sub _inactivity_timeout {
return unless my $stream = Mojo::IOLoop->stream(shift->tx->connection // '');
$stream->timeout(shift);
@@ -319,6 +366,25 @@ L</"stash">.
Alias for L<Mojolicious::Controller/"param">.
+=head2 render_exception
+
+ $c = $c->render_exception('Oops!');
+ $c = $c->render_exception(Mojo::Exception->new('Oops!'));
+
+Render the exception template C<exception.$mode.$format.*> or
+C<exception.$format.*> and set the response status code to C<500>. Also sets
+the stash values C<exception> to a L<Mojo::Exception> object and C<snapshot>
+to a copy of the L</"stash"> for use in the templates.
+
+=head2 render_not_found
+
+ $c = $c->render_not_found;
+
+Render the not found template C<not_found.$mode.$format.*> or
+C<not_found.$format.*> and set the response status code to C<404>. Also sets
+the stash value C<snapshot> to a copy of the L</"stash"> for use in the
+templates.
+
=head2 session
%= session 'foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment