Skip to content

Commit

Permalink
Ignore a layout set in the stash when rendering an inline template.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkende committed Jun 10, 2021
1 parent 445bda8 commit 74be0c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/Mojolicious/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
14 changes: 12 additions & 2 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 @@ -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 {
Expand Down

0 comments on commit 74be0c5

Please sign in to comment.