Skip to content

Commit

Permalink
added render_maybe method to Mojolicious::Controller and removed a fe…
Browse files Browse the repository at this point in the history
…w render_* alias methods
  • Loading branch information
kraih committed May 14, 2013
1 parent 93f9536 commit 67c71b8
Show file tree
Hide file tree
Showing 33 changed files with 141 additions and 178 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@
- Code name "Top Hat", this is a major release.
- Added simple JSON serialization support to Mojo::Transaction::WebSocket.
- Added json event to Mojo::Transaction::WebSocket.
- Added render_maybe method to Mojolicious::Controller.
- Added again method to Mojo::Reactor, Mojo::Reactor::Poll and
Mojo::Reactor::EV.
- Added is_empty method to Mojo::Transaction::HTTP.
Expand All @@ -20,6 +21,8 @@
- Removed captures attribute from Mojolicious::Routes::Match.
- Removed charset attribute from Mojo::DOM::HTML.
- Removed charset method from Mojo::DOM.
- Removed render_data, render_json, render_partial and render_text methods
from Mojolicious::Controller.
- Removed has_leftovers method from Mojo::Content.
- Removed is_chunked, is_dynamic, is_multipart, has_leftovers, leftovers,
write and write_chunk methods from Mojo::Message.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious.pm
Expand Up @@ -70,10 +70,9 @@ sub new {

# Hide controller attributes/methods and "handler"
$r->hide(qw(AUTOLOAD DESTROY app cookie finish flash handler on param));
$r->hide(qw(redirect_to render render_data render_exception render_json));
$r->hide(qw(render_not_found render_partial render_static render_text));
$r->hide(qw(rendered req res respond_to send session signed_cookie stash));
$r->hide(qw(tx ua url_for write write_chunk));
$r->hide(qw(redirect_to render render_exception render_not_found));
$r->hide(qw(render_static rendered req res respond_to send session));
$r->hide(qw(signed_cookie stash tx ua url_for write write_chunk));

# Check if we have a log directory
my $mode = $self->mode;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/generate/plugin.pm
Expand Up @@ -86,7 +86,7 @@ plugin '<%= $name %>';
get '/' => sub {
my $self = shift;
$self->render_text('Hello Mojo!');
$self->render(text => 'Hello Mojo!');
};
my $t = Test::Mojo->new;
Expand Down
104 changes: 31 additions & 73 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -150,14 +150,18 @@ sub render {

# Template may be first argument
my $template = @_ % 2 && !ref $_[0] ? shift : undef;
my $args = ref $_[0] ? $_[0] : {@_};
my $args = {@_};
$args->{template} = $template if $template;
my $maybe = delete $args->{'mojo.maybe'};

# Render
my $app = $self->app;
my ($output, $format) = $app->renderer->render($self, $args);
return undef unless defined $output;
return Mojo::ByteStream->new($output) if $args->{partial};
return defined $output ? Mojo::ByteStream->new($output) : undef
if $args->{partial};

# Maybe
return $maybe ? undef : !!$self->render_not_found unless defined $output;

# Prepare response
$app->plugins->emit_hook(after_render => $self, \$output, $format);
Expand All @@ -167,8 +171,6 @@ sub render {
return !!$self->rendered($self->stash->{status});
}

sub render_data { shift->render(data => @_) }

sub render_exception {
my ($self, $e) = @_;

Expand Down Expand Up @@ -197,10 +199,10 @@ sub render_exception {
$self->_fallbacks({%$options, format => 'html'}, 'exception', $inline);
}

sub render_json { shift->render(json => @_) }

sub render_later { shift->stash('mojo.rendered' => 1) }

sub render_maybe { shift->render(@_, 'mojo.maybe' => 1) }

sub render_not_found {
my $self = shift;

Expand All @@ -217,13 +219,6 @@ sub render_not_found {
$self->_fallbacks({%$options, format => 'html'}, 'not_found', $inline);
}

sub render_partial {
my $self = shift;
my $template = @_ % 2 ? shift : undef;
return $self->render(
{@_, partial => 1, defined $template ? (template => $template) : ()});
}

sub render_static {
my ($self, $file) = @_;
my $app = $self->app;
Expand All @@ -232,8 +227,6 @@ sub render_static {
return undef;
}

sub render_text { shift->render(text => @_) }

sub rendered {
my ($self, $status) = @_;

Expand Down Expand Up @@ -295,7 +288,7 @@ sub respond_to {
}

# Dispatch
ref $target eq 'CODE' ? $target->($self) : $self->render($target);
ref $target eq 'CODE' ? $target->($self) : $self->render(%$target);
}

sub send {
Expand Down Expand Up @@ -446,18 +439,18 @@ sub _fallbacks {
my ($self, $options, $template, $inline) = @_;

# Mode specific template
return 1 if $self->render($options);
return 1 if $self->render_maybe(%$options);

# Normal template
$options->{template} = $template;
return 1 if $self->render($options);
return 1 if $self->render_maybe(%$options);

# Inline template
my $stash = $self->stash;
return undef unless $stash->{format} eq 'html';
delete $stash->{$_} for qw(extends layout);
delete $options->{template};
return $self->render(%$options, inline => $inline, handler => 'ep');
return $self->render_maybe(%$options, inline => $inline, handler => 'ep');
}

1;
Expand Down Expand Up @@ -650,7 +643,6 @@ Prepare a C<302> redirect response, takes the same arguments as C<url_for>.
my $success = $c->render;
my $success = $c->render(controller => 'foo', action => 'bar');
my $success = $c->render({controller => 'foo', action => 'bar'});
my $success = $c->render(template => 'foo/index');
my $success = $c->render(template => 'index', format => 'html');
my $success = $c->render(data => $bytes);
Expand All @@ -665,17 +657,6 @@ C<after_render> hook unless the result is C<partial>. If no template is
provided a default one based on controller and action or route name will be
generated, all additional values get merged into the C<stash>.
=head2 render_data
$c->render_data($bytes);
$c->render_data($bytes, format => 'png');
Render the given content as bytes, similar to C<render_text> but data will not
be encoded. All additional values get merged into the C<stash>.
# Longer version
$c->render(data => $bytes);
=head2 render_exception
$c->render_exception('Oops!');
Expand All @@ -686,17 +667,6 @@ 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 C<stash> for use in the templates.
=head2 render_json
$c->render_json({foo => 'bar'});
$c->render_json([1, 2, -3], status => 201);
Render a data structure as JSON. All additional values get merged into the
C<stash>.
# Longer version
$c->render(json => {foo => 'bar'});
=head2 render_later
$c = $c->render_later;
Expand All @@ -710,24 +680,28 @@ automatic rendering would result in a response.
$c->render(text => 'Delayed by 2 seconds!');
});
=head2 render_maybe
my $success = $c->render_maybe;
my $success = $c->render_maybe(controller => 'foo', action => 'bar');
my $success = $c->render_maybe(template => 'foo/index');
my $success = $c->render_maybe(template => 'index', format => 'html');
my $success = $c->render_maybe(data => $bytes);
my $success = $c->render_maybe(text => 'Hello!');
my $success = $c->render_maybe(json => {foo => 'bar'});
my $success = $c->render_maybe(handler => 'something');
my $success = $c->render_maybe('foo/index');
Try to render content, takes the same arguments as C<render> but does not call
C<render_not_found> if rendering fails.
=head2 render_not_found
$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>.
=head2 render_partial
my $output = $c->render_partial('menubar');
my $output = $c->render_partial('menubar', format => 'txt');
my $output = $c->render_partial(template => 'menubar');
Same as C<render> but returns the rendered result.
# Longer version
my $output = $c->render('menubar', partial => 1);
=head2 render_static
my $success = $c->render_static('images/logo.png');
Expand All @@ -737,22 +711,6 @@ Render a static file using L<Mojolicious::Static/"serve">, usually from the
C<public> directories or C<DATA> sections of your application. Note that this
method does not protect from traversing to parent directories.
=head2 render_text
$c->render_text('Hello World!');
$c->render_text('Hello World!', layout => 'green');
Render the given content as characters, which will be encoded to bytes. All
additional values get merged into the C<stash>. See C<render_data> for an
alternative without encoding. Note that this does not change the content type
of the response, which is C<text/html;charset=UTF-8> by default.
# Longer version
$c->render(text => 'Hello World!');
# Render "text/plain" response
$c->render_text('Hello World!', format => 'txt');
=head2 rendered
$c = $c->rendered;
Expand Down Expand Up @@ -807,7 +765,7 @@ more than one MIME type will be ignored, unless the C<X-Requested-With> header
is set to the value C<XMLHttpRequest>.
$c->respond_to(
json => sub { $c->render_json({just => 'works'}) },
json => sub { $c->render(json => {just => 'works'}) },
xml => {text => '<just>works</just>'},
any => {data => '', status => 204}
);
Expand Down Expand Up @@ -915,13 +873,13 @@ Get L<Mojo::UserAgent> object from L<Mojo/"ua">.
# Non-blocking
$c->ua->get('http://example.com' => sub {
my ($ua, $tx) = @_;
$c->render_data($tx->res->body);
$c->render(data => $tx->res->body);
});
# Parallel non-blocking
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @titles) = @_;
$c->render_json(\@titles);
$c->render(json => \@titles);
});
for my $url ('http://mojolicio.us', 'https://metacpan.org') {
my $end = $delay->begin(0);
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -354,7 +354,7 @@ L<Mojolicious::Controller/"render_not_found">.
return $self->render_exception('Division by zero!') if $divisor == 0;

# 200
$self->render_text($dividend / $divisor);
$self->render(text => $dividend / $divisor);
};

app->start;
Expand Down Expand Up @@ -681,7 +681,7 @@ The C<register> method will be called when you load the plugin.
get '/' => sub {
my $self = shift;
$self->debug('It works.');
$self->render_text('Hello.');
$self->render(text => 'Hello.');
};

app->start;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -837,7 +837,7 @@ self-contained applications under a prefix.
plugin Mount => {'/prefix' => '/home/sri/myapp.pl'};

# Normal route
get '/' => sub { shift->render_text('Hello World!') };
get '/' => sub { shift->render(text => 'Hello World!') };

app->start;

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -638,9 +638,9 @@ Restrictive placeholders can also be used.
# /hello.txt
get '/hello' => [format => [qw(json txt)]] => sub {
my $self = shift;
return $self->render_json({hello => 'world'})
return $self->render(json => {hello => 'world'})
if $self->stash('format') eq 'json';
$self->render_text('hello world');
$self->render(text => 'hello world');
};
app->start;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -103,7 +103,7 @@ sub _include {
my @keys = keys %$args;
local @{$self->stash}{@keys} = @{$args}{@keys};

return $self->render_partial(layout => $layout, extend => $extends);
return $self->render(partial => 1, layout => $layout, extend => $extends);
}

sub _url_with {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -21,7 +21,7 @@ sub auto_render {
my ($self, $c) = @_;
my $stash = $c->stash;
return if $stash->{'mojo.rendered'};
$c->render or $stash->{'mojo.routed'} or $c->render_not_found;
$c->render_maybe or $stash->{'mojo.routed'} or $c->render_not_found;
}

sub dispatch {
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/app.t
Expand Up @@ -90,7 +90,7 @@ $app->routes->post(
$local_port = $self->tx->local_port;
$remote_address = $self->tx->remote_address;
$remote_port = $self->tx->remote_port;
$self->render_data($self->req->upload('file')->slurp);
$self->render(data => $self->req->upload('file')->slurp);
}
);

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/cgi.t
Expand Up @@ -29,7 +29,7 @@ post '/chunked' => sub {

get '/params' => sub {
my $self = shift;
$self->render_json($self->req->params->to_hash);
$self->render(json => $self->req->params->to_hash);
};

# Simple
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/psgi.t
Expand Up @@ -19,12 +19,12 @@ get '/cookies' => sub {
my $self = shift;
my $params = $self->req->params->to_hash;
for my $key (sort keys %$params) { $self->cookie($key, $params->{$key}) }
$self->render_text('nomnomnom');
$self->render(text => 'nomnomnom');
};

post '/params' => sub {
my $self = shift;
$self->render_json($self->req->params->to_hash);
$self->render(json => $self->req->params->to_hash);
};

# Binding
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/user_agent.t
Expand Up @@ -38,12 +38,12 @@ get '/echo' => sub {
my $self = shift;
gzip \(my $uncompressed = $self->req->body), \my $compressed;
$self->res->headers->content_encoding($self->req->headers->accept_encoding);
$self->render_data($compressed);
$self->render(data => $compressed);
};

post '/echo' => sub {
my $self = shift;
$self->render_data($self->req->body);
$self->render(data => $self->req->body);
};

# Proxy detection
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/websocket_proxy.t
Expand Up @@ -18,12 +18,12 @@ get '/' => sub {
my $self = shift;
my $rel = $self->req->url;
my $abs = $rel->to_abs;
$self->render_text("Hello World! $rel $abs");
$self->render(text => "Hello World! $rel $abs");
};

get '/proxy' => sub {
my $self = shift;
$self->render_text($self->req->url);
$self->render(text => $self->req->url);
};

websocket '/test' => sub {
Expand Down

0 comments on commit 67c71b8

Please sign in to comment.