Skip to content

Commit ec7966c

Browse files
committed
Allow inline templates to use layouts when set explicitly in render().
1 parent c05b4ea commit ec7966c

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

lib/Mojolicious/Controller.pm

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

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

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

144145
# All other arguments just become part of the stash
145146
@$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

lib/Mojolicious/Renderer.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ sub render {
108108
# Inheritance
109109
my $content = $stash->{'mojo.content'} //= {};
110110
local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout};
111-
while ((my $next = _next($stash)) && !defined $inline) {
111+
delete $options->{inline};
112+
while ((my $next = _next($stash))) {
112113
@$options{qw(handler template)} = ($stash->{handler}, $next);
113114
$options->{format} = $stash->{format} || $self->default_format;
114115
if ($self->_render_template($c, \my $tmp, $options)) { $output = $tmp }

t/mojolicious/layouted_lite_app.t

Lines changed: 10 additions & 0 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};
@@ -254,6 +259,11 @@ subtest 'Inline template' => sub {
254259
$t->get_ok('/inline')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("inline!\n");
255260
};
256261

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");
265+
};
266+
257267
subtest '"0" inline template' => sub {
258268
$t->get_ok('/inline/again')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is("0\n");
259269
};

0 commit comments

Comments
 (0)