Skip to content

Instantly share code, notes, and snippets.

Created November 10, 2015 13:03
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/a76ffbb08b6b80b99aeb to your computer and use it in GitHub Desktop.
Save anonymous/a76ffbb08b6b80b99aeb 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;
get '/' => 'third';
app->start;
__DATA__
@@ layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head><title><%= $title %></title></head>
<body><%= content %></body>
</html>
@@ first.html.ep
layout 'mylayout', title => 'Hello';
%= 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