Skip to content

Commit 2d74508

Browse files
committed
Add an inline_layout stash keyword, based on the mojo.js inlineLayout one.
1 parent c05b4ea commit 2d74508

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/Mojolicious/Guides/Rendering.pod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ as a render argument (not a stash value).
530530

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

533+
For renderers that support C<inline> templates, such as C<ep>, you can also pass a layout with the C<inline_layout>
534+
stash value.
535+
536+
$c->render(inline => 'World', inline_layout => 'Hello <%= content %>!');
537+
533538
=head2 Partial templates
534539

535540
You can break up bigger templates into smaller, more manageable chunks. These partial templates can also be shared with

lib/Mojolicious/Renderer.pm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ sub render {
108108
# Inheritance
109109
my $content = $stash->{'mojo.content'} //= {};
110110
local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout};
111+
if ($stash->{inline_layout}) {
112+
@$options{qw(template inline)} = (undef, delete $stash->{inline_layout});
113+
$options->{format} = $stash->{format} || $self->default_format;
114+
if ($self->_render_template($c, \my $tmp, $options)) { $output = $tmp }
115+
$content->{content} //= $output if $output =~ /\S/;
116+
}
111117
while ((my $next = _next($stash)) && !defined $inline) {
112118
@$options{qw(handler template)} = ($stash->{handler}, $next);
113119
$options->{format} = $stash->{format} || $self->default_format;
@@ -214,6 +220,15 @@ sub _next {
214220
return join '/', 'layouts', $layout;
215221
}
216222

223+
sub _render_content {
224+
my ($self, $c, $next, $options, $output) = @_;
225+
my $stash = $c->stash;
226+
@$options{qw(handler template)} = ($stash->{handler}, $next);
227+
$options->{format} = $stash->{format} || $self->default_format;
228+
if ($self->_render_template($c, \my $tmp, $options)) { $$output = $tmp }
229+
$stash->{'mojo.content'}->{content} //= $$output if $$output =~ /\S/;
230+
}
231+
217232
sub _render_template {
218233
my ($self, $c, $output, $options) = @_;
219234

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/inline_layout' => sub {
101+
my $c = shift;
102+
$c->render(inline => '<%= "inline!" %>', inline_layout => 'layouted_inline <%== content %>');
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/inline_layout')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
264+
->content_is("layouted_inline 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)