Skip to content

Commit

Permalink
Allow inline templates to use layouts when set explicitly in render().
Browse files Browse the repository at this point in the history
  • Loading branch information
mkende committed Oct 15, 2021
1 parent 84f9d79 commit 70dc426
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/Mojolicious/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,9 @@ To set a C<layout> stash value application-wide you can use L<Mojolicious/"defau

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

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

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

Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Renderer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions t/mojolicious/layouted_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
};
Expand Down

0 comments on commit 70dc426

Please sign in to comment.