Skip to content

Commit

Permalink
fix bug in Mojolicious::Renderer where layouts could not be used with…
Browse files Browse the repository at this point in the history
… template inheritance
  • Loading branch information
kraih committed Nov 10, 2015
1 parent 86e4438 commit 069ac5b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 46 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

6.30 2015-11-10
- Fixed bug in Mojolicious::Renderer where layouts could not be used with
template inheritance. (nic, sri)

6.29 2015-11-03
- Fixed a few bugs in built-in templates. (Zoffix, sri)
Expand Down
91 changes: 48 additions & 43 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -769,49 +769,6 @@ also be used to pass whole sections of the template to the layout.
<body><%= content %></body>
</html>

=head2 Template inheritance

Inheritance is an alternative to the layout concept above, and takes it one step
further. The helpers L<Mojolicious::Plugin::DefaultHelpers/"content"> and
L<Mojolicious::Plugin::DefaultHelpers/"extends"> allow you to build a skeleton
template with named blocks that child templates can override.

use Mojolicious::Lite;

get '/' => 'third';

app->start;
__DATA__

@@ first.html.ep
<!DOCTYPE html>
<html>
<head><title>Hello</title></head>
<body>
%= content header => begin
Default header
% end
<div>Hello World!</div>
%= content footer => begin
Default footer
% end
</body>
</html>

@@ second.html.ep
% extends 'first';
% content header => begin
New header
% end

@@ third.html.ep
% extends 'second';
% content footer => begin
New footer
% end

This chain could go on and on to allow a very high level of template reuse.

=head2 Forms

To build HTML forms more efficiently you can use tag helpers like
Expand Down Expand Up @@ -1034,6 +991,54 @@ The token can also be submitted with the C<X-CSRF-Token> request header.

Less commonly used and more powerful features.

=head2 Template inheritance

Inheritance takes the layout concept above one step further, the helpers
L<Mojolicious::Plugin::DefaultHelpers/"content"> and
L<Mojolicious::Plugin::DefaultHelpers/"extends"> allow you to build skeleton
templates with named blocks that child templates can override.

use Mojolicious::Lite;

# first > mylayout
get '/first' => {template => 'first', layout => 'mylayout'};

# third > second > first > mylayout
get '/third' => {template => 'third', layout => 'mylayout'};

app->start;
__DATA__

@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><title>Hello</title></head>
<body><%= content %></body>
</html>

@@ first.html.ep
%= content header => begin
Default header
% end
<div>Hello World!</div>
%= content footer => begin
Default footer
% end

@@ second.html.ep
% extends 'first';
% content header => begin
New header
% end

@@ third.html.ep
% extends 'second';
% content footer => begin
New footer
% end

This chain could go on and on to allow a very high level of template reuse.

=head2 Serving static files

Static files are automatically served from the C<public> directories of the
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -226,8 +226,8 @@ sub warmup {

sub _extends {
my ($self, $stash) = @_;
my $layout = delete $stash->{layout};
$stash->{extends} ||= join('/', 'layouts', $layout) if $layout;
$stash->{extends} ||= join('/', 'layouts', delete $stash->{layout})
if $stash->{layout};
return delete $stash->{extends};
}

Expand Down
46 changes: 45 additions & 1 deletion t/mojolicious/layouted_lite_app.t
Expand Up @@ -48,6 +48,12 @@ get '/double_inheritance' =>

get '/triple_inheritance';

get '/mixed_inheritance/first' => {template => 'first'};

get '/mixed_inheritance/second' => {template => 'second', layout => 'green'};

get '/mixed_inheritance/third' => {template => 'third'};

get '/nested-includes' => sub {
my $c = shift;
$c->render(
Expand All @@ -71,7 +77,11 @@ get '/outerlayout' => sub {

get '/outerextends' => sub {
my $c = shift;
$c->render(template => 'outerlayout', extends => 'layouts/layout');
$c->render(
template => 'outerlayout',
extends => 'layouts/layout',
layout => undef
);
};

get '/outerlayouttwo' => {layout => 'layout'} => sub {
Expand Down Expand Up @@ -188,6 +198,17 @@ $t->get_ok('/triple_inheritance')->status_is(200)
->content_is("<title>Works!</title>\n<br>\nSidebar too!\n"
. "New <content>.\n\nDefault footer!\n");

# Mixed inheritance (with layout)
$t->get_ok('/mixed_inheritance/first')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("Default\n Default header\nStuff\n\n Default footer\n\n");
$t->get_ok('/mixed_inheritance/second')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("Green New header\nStuff\n\n Default footer\n\n");
$t->get_ok('/mixed_inheritance/third')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("Default New header\nStuff\n New footer\n\n");

# Template from plugin
$t->get_ok('/plugin_with_template')->status_is(200)
->content_is("layout_with_template\nwith template\n\n");
Expand Down Expand Up @@ -285,6 +306,7 @@ Green<%= title %><%= content %>
Mixed <%= content %>
@@ blue.html.ep
% layout undef;
Blue<%= title %><%= content %>
@@ works.html.ep
Expand All @@ -309,6 +331,27 @@ Exception happened!
% extends 'blue' if param 'blue';
Not found happened!
@@ first.html.ep
%= content header => begin
Default header
% end
Stuff
%= content footer => begin
Default footer
% end
@@ second.html.ep
% extends 'first';
% content header => begin
New header
% end
@@ third.html.ep
% extends 'second';
% content footer => begin
New footer
% end
@@ template_inheritance.html.ep
% layout 'template_inheritance';
% title 'Works!';
Expand Down Expand Up @@ -353,6 +396,7 @@ Nested <%= include 'outerlayout' %>
@@ localized.html.ep
% extends 'localized1';
% layout undef;
<%= $test %>
<%= include 'localized_include', test => 321, extends => 'localized2' %>
<%= $test %>
Expand Down

0 comments on commit 069ac5b

Please sign in to comment.