diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm index 7e2d13981b..fc2e2254ea 100644 --- a/lib/Mojolicious/Controller.pm +++ b/lib/Mojolicious/Controller.pm @@ -133,13 +133,14 @@ sub render { # Localize "extends" and "layout" to allow argument overrides my ($maybe, $ts) = @{$args}{'mojo.maybe', 'mojo.string'}; - my $stash = $self->stash; + my $stash = $self->stash; + my $inline = exists $args->{inline} || exists $stash->{inline}; local $stash->{layout} = $stash->{layout} if exists $stash->{layout}; local $stash->{extends} = $stash->{extends} if exists $stash->{extends}; - # Rendering to string + # Rendering to string or using inline templates local @{$stash}{keys %$args} if $ts || $maybe; - delete @{$stash}{qw(layout extends)} if $ts; + delete @{$stash}{qw(layout extends)} if $ts || $inline; # All other arguments just become part of the stash @$stash{keys %$args} = values %$args; diff --git a/lib/Mojolicious/Guides/Rendering.pod b/lib/Mojolicious/Guides/Rendering.pod index b173277028..a4ed30fbe7 100644 --- a/lib/Mojolicious/Guides/Rendering.pod +++ b/lib/Mojolicious/Guides/Rendering.pod @@ -525,8 +525,9 @@ To set a C stash value application-wide you can use Ldefaults(layout => 'mylayout'); -Layouts can also be used with L, but the C value needs to be passed -as a render argument (not a stash value). +Layouts can also be used with L, or when rendering +L, but the C value needs to be passed as a render argument +(not a stash value). my $html = $c->render_to_string('reminder', layout => 'mail'); diff --git a/lib/Mojolicious/Renderer.pm b/lib/Mojolicious/Renderer.pm index 1e7dd70d1f..ed811336f9 100644 --- a/lib/Mojolicious/Renderer.pm +++ b/lib/Mojolicious/Renderer.pm @@ -108,7 +108,8 @@ sub render { # Inheritance my $content = $stash->{'mojo.content'} //= {}; local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout}; - while ((my $next = _next($stash)) && !defined $inline) { + delete $options->{inline}; + while ((my $next = _next($stash))) { @$options{qw(handler template)} = ($stash->{handler}, $next); $options->{format} = $stash->{format} || $self->default_format; if ($self->_render_template($c, \my $tmp, $options)) { $output = $tmp } diff --git a/t/mojolicious/layouted_lite_app.t b/t/mojolicious/layouted_lite_app.t index 8756f96587..8976b5e6e7 100644 --- a/t/mojolicious/layouted_lite_app.t +++ b/t/mojolicious/layouted_lite_app.t @@ -97,6 +97,11 @@ get '/content_with'; get '/inline' => {inline => '<%= "inline!" %>'}; +get '/inline/explicit_layout' => sub { + my $c = shift; + $c->render(inline => '<%= "inline!" %>', layout => 'layout'); +}; + get '/inline/again' => {inline => 0}; get '/data' => {data => 0}; @@ -254,6 +259,11 @@ subtest 'Inline template' => sub { $t->get_ok('/inline')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("inline!\n"); }; +subtest 'Inline with layout' => sub { + $t->get_ok('/inline/explicit_layout')->status_is(200)->header_is(Server => 'Mojolicious (Perl)') + ->content_is("layouted inline!\n\n"); +}; + subtest '"0" inline template' => sub { $t->get_ok('/inline/again')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("0\n"); };