Skip to content

Commit 74be0c5

Browse files
committed
Ignore a layout set in the stash when rendering an inline template.
1 parent 445bda8 commit 74be0c5

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/Mojolicious/Controller.pm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,14 @@ sub render {
132132

133133
# Localize "extends" and "layout" to allow argument overrides
134134
my ($maybe, $ts) = @{$args}{'mojo.maybe', 'mojo.string'};
135-
my $stash = $self->stash;
135+
my $stash = $self->stash;
136+
my $inline = exists $args->{inline} || exists $stash->{inline};
136137
local $stash->{layout} = $stash->{layout} if exists $stash->{layout};
137138
local $stash->{extends} = $stash->{extends} if exists $stash->{extends};
138139

139-
# Rendering to string
140+
# Rendering to string or using inline templates
140141
local @{$stash}{keys %$args} if $ts || $maybe;
141-
delete @{$stash}{qw(layout extends)} if $ts;
142+
delete @{$stash}{qw(layout extends)} if $ts || $inline;
142143

143144
# All other arguments just become part of the stash
144145
@$stash{keys %$args} = values %$args;

lib/Mojolicious/Guides/Rendering.pod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,9 @@ To set a C<layout> stash value application-wide you can use L<Mojolicious/"defau
525525

526526
$app->defaults(layout => 'mylayout');
527527

528-
Layouts can also be used with L<Mojolicious::Controller/"render_to_string">, but the C<layout> value needs to be passed
529-
as a render argument (not a stash value).
528+
Layouts can also be used with L<Mojolicious::Controller/"render_to_string">, or when rendering
529+
L<inline templates|/"Rendering inline templates">, but the C<layout> value needs to be passed as a render argument
530+
(not a stash value).
530531

531532
my $html = $c->render_to_string('reminder', layout => 'mail');
532533

t/mojolicious/layouted_lite_app.t

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ get '/content_with';
9797

9898
get '/inline' => {inline => '<%= "inline!" %>'};
9999

100+
get '/inline/explicit_layout' => sub {
101+
my $c = shift;
102+
$c->render(inline => '<%= "inline!" %>', layout => 'layout');
103+
};
104+
100105
get '/inline/again' => {inline => 0};
101106

102107
get '/data' => {data => 0};
@@ -251,11 +256,16 @@ subtest 'Content blocks' => sub {
251256
};
252257

253258
subtest 'Inline template' => sub {
254-
$t->get_ok('/inline')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("Defaultinline!\n\n");
259+
$t->get_ok('/inline')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("inline!\n");
260+
};
261+
262+
subtest 'Inline with layout' => sub {
263+
$t->get_ok('/inline/explicit_layout')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
264+
->content_is("layouted inline!\n\n");
255265
};
256266

257267
subtest '"0" inline template' => sub {
258-
$t->get_ok('/inline/again')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("Default0\n\n");
268+
$t->get_ok('/inline/again')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("0\n");
259269
};
260270

261271
subtest '"0" data' => sub {

0 commit comments

Comments
 (0)