Skip to content

Instantly share code, notes, and snippets.

Created November 10, 2015 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a1c4d4a698f5f3648483 to your computer and use it in GitHub Desktop.
Save anonymous/a1c4d4a698f5f3648483 to your computer and use it in GitHub Desktop.
=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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment