Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an inline_layout stash keyword, based on the mojo.js inlineLayout one. #1887

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Mojolicious/Guides/Rendering.pod
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ as a render argument (not a stash value).

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

For renderers that support C<inline> templates, such as C<ep>, you can also pass a layout with the C<inline_layout>
stash value.

$c->render(inline => 'World', inline_layout => 'Hello <%= content %>!');

mkende marked this conversation as resolved.
Show resolved Hide resolved
=head2 Partial templates

You can break up bigger templates into smaller, more manageable chunks. These partial templates can also be shared with
Expand Down
9 changes: 8 additions & 1 deletion lib/Mojolicious/Renderer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ sub render {

# Inheritance
my $content = $stash->{'mojo.content'} //= {};
local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout};
local $content->{content} = $output =~ /\S/ ? $output : undef
if $stash->{extends} || $stash->{layout} || $stash->{inline_layout};
if (defined $stash->{inline_layout}) {
local $options->{inline} = delete $stash->{inline_layout};
delete $stash->{layout};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the tests still pass if this line is removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do. I tried to write something to test this line but did not succeed (setting layout and inline_layout together in a call to render results in an error 500 but I’m not sure why). I still have the impression that this line should stay.

Do you have an idea of how to test it? Or do you think that it should be removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code that does nothing should not be present.

if ($self->_render_template($c, \my $tmp, $options)) { $output = $tmp }
$content->{content} //= $output if $output =~ /\S/;
mkende marked this conversation as resolved.
Show resolved Hide resolved
}
while ((my $next = _next($stash)) && !defined $inline) {
@$options{qw(handler template)} = ($stash->{handler}, $next);
$options->{format} = $stash->{format} || $self->default_format;
Expand Down
19 changes: 19 additions & 0 deletions t/mojolicious/layouted_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ get '/content_with';

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

get '/inline/inline_layout' => sub {
my $c = shift;
$c->render(inline => '<%= "inline!" %>', inline_layout => 'layouted_inline <%== content %>');
};

get '/inline/template_with_inline_layout' => sub {
my $c = shift;
$c->render(template => 'variants', format => 'txt', inline_layout => 'layouted_inline <%== content %>');
};

get '/inline/again' => {inline => 0};

get '/data' => {data => 0};
Expand Down Expand Up @@ -254,6 +264,15 @@ 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/inline_layout')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->content_is("layouted_inline inline!\n\n");
};

subtest 'Template with inline layout' => sub {
$t->get_ok('/inline/template_with_inline_layout')->status_is(200)->content_is("layouted_inline Desktop!\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