Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
replaced reserved stash value partial with render_partial method
  • Loading branch information
kraih committed May 27, 2014
1 parent 506b8ba commit e3941a2
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 72 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -9,6 +9,7 @@
- Removed return values from wait method in Mojo::IOLoop::Delay.
- Removed generate_port method from Mojo::IOLoop.
- Removed check_file method from Mojo::Server::Morbo.
- Replaced reserved stash value partial with render_partial method.
- Replaced format method in Mojo::Log with an attribute.
- Added with_compression method to Mojo::Transaction::WebSocket.
- Added catch method to Mojo::EventEmitter.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -534,7 +534,7 @@ new ones.
Build default controller object with L</"controller_class">.
# Render template from application
my $foo = $app->build_controller->render(template => 'foo', partial => 1);
my $foo = $app->build_controller->render_partial(template => 'foo');
=head2 build_tx
Expand Down
42 changes: 30 additions & 12 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -21,7 +21,7 @@ has tx => sub { Mojo::Transaction::HTTP->new };
# Reserved stash values
my %RESERVED = map { $_ => 1 } (
qw(action app cb controller data extends format handler json layout),
qw(namespace partial path status template text variant)
qw(namespace path status template text variant)
);

sub AUTOLOAD {
Expand Down Expand Up @@ -152,7 +152,7 @@ sub render {
my $maybe = delete $args->{'mojo.maybe'};

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

Expand All @@ -175,6 +175,18 @@ sub render_maybe { shift->render(@_, 'mojo.maybe' => 1) }

sub render_not_found { _development('not_found', @_) }

sub render_partial {
my $self = shift;

# Template may be first argument
my ($template, $args) = (@_ % 2 ? shift : undef, {@_});
$args->{template} = $template if $template;

# Localize arguments
local @{$self->stash}{keys %$args};
return $self->render(%$args, 'mojo.partial' => 1);
}

sub render_static {
my ($self, $file) = @_;
my $app = $self->app;
Expand Down Expand Up @@ -642,14 +654,12 @@ Prepare a C<302> redirect response, takes the same arguments as L</"url_for">.
my $bool = $c->render(json => {foo => 'bar'});
my $bool = $c->render(handler => 'something');
my $bool = $c->render('foo/index');
my $output = $c->render('foo/index', partial => 1);
Render content using L<Mojolicious::Renderer/"render"> and emit hooks
L<Mojolicious/"before_render"> as well as L<Mojolicious/"after_render"> if the
result is not C<partial>. If no template is provided a default one based on
controller and action or route name will be generated with
L<Mojolicious::Renderer/"template_for">, all additional values get merged into
the L</"stash">.
L<Mojolicious/"before_render"> as well as L<Mojolicious/"after_render">. If no
template is provided a default one based on controller and action or route
name will be generated with L<Mojolicious::Renderer/"template_for">, all
additional values get merged into the L</"stash">.
# Render characters
$c->render(text => 'I ♥ Mojolicious!');
Expand Down Expand Up @@ -699,7 +709,7 @@ automatic rendering would result in a response.
my $bool = $c->render_maybe(controller => 'foo', action => 'bar');
my $bool = $c->render_maybe('foo/index', format => 'html');
Try to render content but do not call L</"render_not_found"> if no response
Try to render content, but do not call L</"render_not_found"> if no response
could be generated, takes the same arguments as L</"render">.
# Render template "index_local" only if it exists
Expand All @@ -714,6 +724,14 @@ 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_partial
my $output = $c->render_partial('foo/index', format => 'pdf');
Try to render content and return it, all arguments get localized automatically
and are only available in the partial template, takes the same arguments as
L</"render">.
=head2 render_static
my $bool = $c->render_static('images/logo.png');
Expand Down Expand Up @@ -884,9 +902,9 @@ Non-persistent data storage and exchange for the current request, application
wide default values can be set with L<Mojolicious/"defaults">. Some stash
values have a special meaning and are reserved, the full list is currently
C<action>, C<app>, C<cb>, C<controller>, C<data>, C<extends>, C<format>,
C<handler>, C<json>, C<layout>, C<namespace>, C<partial>, C<path>, C<status>,
C<template>, C<text> and C<variant>. Note that all stash values with a
C<mojo.*> prefix are reserved for internal use.
C<handler>, C<json>, C<layout>, C<namespace>, C<path>, C<status>, C<template>,
C<text> and C<variant>. Note that all stash values with a C<mojo.*> prefix are
reserved for internal use.
# Remove value
my $foo = delete $c->stash->{foo};
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -315,7 +315,7 @@ with this little L<Mojo::Server> based mock server you can just embed them.
say for @{$app->static->paths};
say $app->config->{secret_identity};
say $app->dumper({just => 'a helper test'});
say $app->build_controller->render(template => 'foo', partial => 1);
say $app->build_controller->render_partial(template => 'foo');

The plugin L<Mojolicious::Plugin::Mount> uses this functionality to allow you
to combine multiple applications into one and deploy them together.
Expand Down
55 changes: 22 additions & 33 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -199,6 +199,23 @@ alternatives.

$self->render_maybe('localized/baz') or $self->render('foo/bar/baz');

=head2 Partial rendering

Sometimes you might want to use the rendered result directly instead of
generating a response, for example to send emails, this can be done with
L<Mojolicious::Controller/"render_partial">.

my $html = $self->render_partial('mail');

No encoding will be performed, making it easy to reuse the result in other
templates or to generate binary data.

my $pdf = $self->render_partial('invoice', format => 'pdf');
$self->render(data => $pdf, format => 'pdf');

All arguments passed will get localized automatically and are only available
in the partial template.

=head2 Template variants

To make your application look great on many different devices you can also
Expand Down Expand Up @@ -244,20 +261,6 @@ renderer which get directly encoded to JSON with L<Mojo::JSON>.

$self->render(json => {foo => [1, 'test', 3]});

=head2 Partial rendering

Sometimes you might want to use the rendered result directly instead of
generating a response, for example to send emails, this can be done using the
C<partial> render argument (not the stash value).

my $html = $self->render('mail', partial => 1);

No encoding will be performed, making it easy to reuse the result in other
templates or to generate binary data.

my $pdf = $self->render('invoice', format => 'pdf', partial => 1);
$self->render(data => $pdf, format => 'pdf');

=head2 Status code

Response status codes can be changed with the C<status> stash value.
Expand Down Expand Up @@ -531,10 +534,10 @@ L<Mojolicious/"defaults">.

1;

Layouts can also be used with C<partial> templates, but the C<layout> value
needs to be passed as a render argument (not a stash value).
Layouts can also be used with partial templates, but the C<layout> value needs
to be passed as a render argument (not a stash value).

my $html = $self->render('reminder', layout => 'mail', partial => 1);
my $html = $self->render_partial('reminder', layout => 'mail');

=head2 Including partial templates

Expand All @@ -552,32 +555,18 @@ shortcut to make your life a little easier.
<head><title>Howdy</title></head>

Instead of C<include> you could also just call
L<Mojolicious::Controller/"render"> with the C<partial> argument.
L<Mojolicious::Controller/"render_partial">.

@@ foo/bar.html.ep
<!DOCTYPE html>
<html>
%= $self->render('header', partial => 1)
%= $self->render_partial('header')
<body>Bar</body>
</html>

@@ header.html.ep
<head><title>Howdy</title></head>

But there is one small difference between the two, if you pass stash values to
C<include>, they will get localized automatically and are only available in
the partial template.

@@ foo/bar.html.ep
<!DOCTYPE html>
<html>
%= include 'header', title => 'Hello'
<body>Bar</body>
</html>

@@ header.html.ep
<head><title><%= $title %></title></head>

=head2 Reusable template blocks

It's never fun to repeat yourself, that's why you can build reusable template
Expand Down
19 changes: 3 additions & 16 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -35,8 +35,8 @@ sub register {
$app->helper(csrf_token => \&_csrf_token);
$app->helper(current_route => \&_current_route);
$app->helper(dumper => sub { shift; dumper(@_) });
$app->helper(include => \&_include);
$app->helper(ua => sub { shift->app->ua });
$app->helper(include => sub { shift->render_partial(@_) });
$app->helper(ua => sub { shift->app->ua });
$app->helper(url_with => \&_url_with);
}

Expand Down Expand Up @@ -77,18 +77,6 @@ sub _current_route {
return $endpoint->name eq shift;
}

sub _include {
my $self = shift;

# Template may be first argument
my ($template, $args) = (@_ % 2 ? shift : undef, {@_});
$args->{template} = $template if $template;

# Localize arguments
local @{$self->stash}{keys %$args};
return $self->render(partial => 1, %$args);
}

sub _url_with {
my $self = shift;
return $self->url_for(@_)->query($self->req->url->query->clone);
Expand Down Expand Up @@ -244,8 +232,7 @@ Alias for L<Mojolicious::Controller/"flash">.
%= include 'menubar'
%= include 'menubar', format => 'txt'
Include a partial template, all arguments get localized automatically and are
only available in the partial template.
Alias for C<Mojolicious::Controller/"render_partial">.
=head2 layout
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -76,7 +76,7 @@ sub render {
local $stash->{layout} = $stash->{layout} if exists $stash->{layout};
local $stash->{extends} = $stash->{extends} if exists $stash->{extends};
delete @{$stash}{qw(layout extends)}
if my $partial = delete $args->{partial};
if my $partial = delete $args->{'mojo.partial'};

# Merge stash and arguments
%$stash = (%$stash, %$args);
Expand Down
5 changes: 2 additions & 3 deletions t/mojolicious/dispatch.t
Expand Up @@ -94,9 +94,8 @@ is $c->param(json => 'test')->param('json'), undef, 'value is reserved';
is $c->param(layout => 'test')->param('layout'), undef, 'value is reserved';
is $c->param(namespace => 'test')->param('namespace'), undef,
'value is reserved';
is $c->param(partial => 'test')->param('partial'), undef, 'value is reserved';
is $c->param(path => 'test')->param('path'), undef, 'value is reserved';
is $c->param(status => 'test')->param('status'), undef, 'value is reserved';
is $c->param(path => 'test')->param('path'), undef, 'value is reserved';
is $c->param(status => 'test')->param('status'), undef, 'value is reserved';
is $c->param(template => 'test')->param('template'), undef,
'value is reserved';
is $c->param(text => 'test')->param('text'), undef, 'value is reserved';
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/external/myapp2.pl
Expand Up @@ -21,7 +21,7 @@
get '/' => sub {
my $self = shift;
$self->render(
text => $self->render('menubar', partial => 1) . app->defaults->{secret});
text => $self->render_partial('menubar') . app->defaults->{secret});
};

get '/cached' => sub {
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/layouted_lite_app.t
Expand Up @@ -367,7 +367,7 @@ localized2 <%= content %>
@@ outerlayout.html.ep
%= c(qw(> o l l e H <))->reverse->join
<%= $self->render('outermenu', partial => 1) %>
<%= $self->render_partial('outermenu') %>
@@ outermenu.html.ep
% stash test => 'there';
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -462,7 +462,7 @@ is $t->app->build_controller->req->url, '', 'no URL';
is $t->app->build_controller->stash->{default}, 23, 'right value';
is $t->app->build_controller($t->app->ua->build_tx(GET => '/foo'))->req->url,
'/foo', 'right URL';
is $t->app->build_controller->render('index', handler => 'epl', partial => 1),
is $t->app->build_controller->render_partial('index', handler => 'epl'),
'Just works!', 'right result';

# Unicode snowman
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/renderer.t
Expand Up @@ -6,7 +6,7 @@ use Mojolicious::Controller;
# Partial rendering
my $c = Mojolicious::Controller->new;
$c->app->log->level('fatal');
is $c->render(text => 'works', partial => 1), 'works', 'renderer is working';
is $c->render_partial(text => 'works'), 'works', 'renderer is working';

# Normal rendering with default format
my $r = $c->app->renderer->default_format('test');
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/templates/layouts/default.html.epl
Expand Up @@ -3,6 +3,6 @@
<html>
<head></head>
<body>
<%= $self->render(partial => 1, template => '23') %><%= $self->content %>
<%= $self->render_partial(template => '23') %><%= $self->content %>
</body>
</html>

0 comments on commit e3941a2

Please sign in to comment.