From 74be0c540dc317abbdef2e3e697e5d8e2a9f0497 Mon Sep 17 00:00:00 2001 From: Mathias Kende Date: Thu, 10 Jun 2021 23:55:40 +0200 Subject: [PATCH] Ignore a layout set in the stash when rendering an inline template. --- lib/Mojolicious/Controller.pm | 7 ++++--- lib/Mojolicious/Guides/Rendering.pod | 5 +++-- t/mojolicious/layouted_lite_app.t | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm index 84f80b868e..3ec073b4d1 100644 --- a/lib/Mojolicious/Controller.pm +++ b/lib/Mojolicious/Controller.pm @@ -132,13 +132,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 4e0a919bb0..ef90ec8ee7 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/t/mojolicious/layouted_lite_app.t b/t/mojolicious/layouted_lite_app.t index 666956cd6f..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}; @@ -251,11 +256,16 @@ subtest 'Content blocks' => sub { }; subtest 'Inline template' => sub { - $t->get_ok('/inline')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("Defaultinline!\n\n"); + $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("Default0\n\n"); + $t->get_ok('/inline/again')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("0\n"); }; subtest '"0" data' => sub {