# -*- coding: utf-8 -*- <%inherit file="content_layout.html"/> <%page args="toc, extension, paged"/> <%namespace name="formatting" file="formatting.html"/> <%namespace name="nav" file="nav.html"/> <%def name="title()">Mako Documentation - Inheritance%def> <%! filename = 'inheritance' %> ## This file is generated. Edit the .txt files instead of this one. <%call expr="formatting.section(path='inheritance',paged=paged,extension=extension,toc=toc)">
Using template inheritance, two or more templates can organize themselves into an inheritance chain, where content and functions from all involved templates can be intermixed. The general paradigm of template inheritance is this: if a template <%text filter='h'>A%text>
inherits from template <%text filter='h'>B%text>
, then template <%text filter='h'>A%text>
agrees to send the executional control to template <%text filter='h'>B%text>
at runtime (<%text filter='h'>A%text>
is called the inheriting template). Template <%text filter='h'>B%text>
, the inherited template, then makes decisions as to what resources from <%text filter='h'>A%text>
shall be executed.
In practice, it looks like this. Heres a hypothetical inheriting template, <%text filter='h'>index.html%text>
:
And <%text filter='h'>base.html%text>
, the inherited template:
Here is a breakdown of the execution:
When <%text filter='h'>index.html%text>
is rendered, control immediately passes to <%text filter='h'>base.html%text>
.
<%text filter='h'>base.html%text>
then renders the top part of an HTML document, then calls the method <%text filter='h'>header()%text>
off of a built in namespace called <%text filter='h'>self%text>
(this namespace was first introduced in the Namespaces chapter in <%call expr="nav.toclink(path='namespaces_builtin_self',paged=paged,extension=extension,toc=toc)">%call>). Since <%text filter='h'>index.html%text>
is the topmost template and also defines a def called <%text filter='h'>header()%text>
, its this <%text filter='h'>header()%text>
def that gets executed.
Control comes back to <%text filter='h'>base.html%text>
. Some more HTML is rendered.
<%text filter='h'>base.html%text>
executes <%text filter='h'>self.body()%text>
. The <%text filter='h'>body()%text>
function on all template-based namespaces refers to the main body of the template, therefore the main body of <%text filter='h'>index.html%text>
is rendered.
Control comes back to <%text filter='h'>base.html%text>
. More HTML is rendered, then the <%text filter='h'>self.footer()%text>
expression is invoked.
The <%text filter='h'>footer%text>
def is only defined in <%text filter='h'>base.html%text>
, so being the topmost definition of <%text filter='h'>footer%text>
, its the one that executes. If <%text filter='h'>index.html%text>
also specified <%text filter='h'>footer%text>
, then its version would override that of the base.
<%text filter='h'>base.html%text>
finishes up rendering its HTML and the template is complete, producing:
...and that is template inheritance in a nutshell. The main idea is that the methods that you call upon <%text filter='h'>self%text>
always correspond to the topmost definition of that method. Very much the way <%text filter='h'>self%text>
works in a Python class, even though Mako is not actually using Python class inheritance to implement this functionality. (Mako doesn't take the "inheritance" metaphor too seriously; while useful to setup some commonly recognized semantics, a textual template is not very much like an object-oriented class construct in practice).
Sometimes you have an inheritance chain that spans more than two templates. Or maybe you don't, but youd like to build your system such that extra inherited templates can be inserted in the middle of a chain where they would be smoothly integrated. If each template wants to define its layout just within its main body, you can't just call <%text filter='h'>self.body()%text>
to get at the inheriting template's body, since that is only the topmost body. To get at the body of the next template, you call upon the namespace <%text filter='h'>next%text>
, which is the namespace of the template immediately following the current template.
Lets change the line in <%text filter='h'>base.html%text>
which calls upon <%text filter='h'>self.body()%text>
to instead call upon <%text filter='h'>next.body()%text>
:
Lets also add an intermediate template called <%text filter='h'>layout.html%text>
, which inherits from <%text filter='h'>base.html%text>
:
And finally change <%text filter='h'>index.html%text>
to inherit from <%text filter='h'>layout.html%text>
instead:
In this setup, each call to <%text filter='h'>next.body()%text>
will render the body of the next template in the inheritance chain (which can be written as <%text filter='h'>base.html -> layout.html -> index.html%text>
). Control is still first passed to the bottommost template <%text filter='h'>base.html%text>
, and <%text filter='h'>self%text>
still references the topmost definition of any particular def.
The output we get would be:
<%call expr="formatting.code()"><%text>So above, we have the <%text filter='h'>%text>
, <%text filter='h'>%text>
and <%text filter='h'>header%text>
/<%text filter='h'>footer%text>
layout of <%text filter='h'>base.html%text>
, we have the <%text filter='h'>
and %text>
<%text filter='h'>mainlayout%text>
section of <%text filter='h'>layout.html%text>
, and the main body of <%text filter='h'>index.html%text>
as well as its overridden <%text filter='h'>header%text>
def. The <%text filter='h'>layout.html%text>
template is inserted into the middle of the chain without <%text filter='h'>base.html%text>
having to change anything. Without the <%text filter='h'>next%text>
namespace, only the main body of <%text filter='h'>index.html%text>
could be used; there would be no way to call <%text filter='h'>layout.html%text>
's body content.
Lets now look at the other inheritance-specific namespace, the opposite of <%text filter='h'>next%text>
called <%text filter='h'>parent%text>
. <%text filter='h'>parent%text>
is the namespace of the template immediately preceding the current template. What is most useful about this namespace is the methods within it which can be accessed within overridden versions of those methods. This is not as hard as it sounds and is very much like using the <%text filter='h'>super%text>
keyword in Python. Lets modify <%text filter='h'>index.html%text>
to augment the list of selections provided by the <%text filter='h'>toolbar%text>
function in <%text filter='h'>layout.html%text>
:
Above, we implemented a <%text filter='h'>toolbar()%text>
function, which is meant to override the definition of <%text filter='h'>toolbar%text>
within the inherited template <%text filter='h'>layout.html%text>
. However, since we want the content from that of <%text filter='h'>layout.html%text>
as well, we call it via the <%text filter='h'>parent%text>
namespace whenever we want it's content, in this case before we add our own selections. So the output for the whole thing is now:
and you're now a template inheritance ninja !
%call> <%call expr="formatting.section(path='inheritance_inheritable',paged=paged,extension=extension,toc=toc)">The <%text filter='h'>attr%text>
accessor of the <%text filter='h'>Namespace%text>
object allows access to module level variables declared in a template. By accessing <%text filter='h'>self.attr%text>
, you can access regular attributes from the inheritance chain as declared in <%text filter='h'><%! %>%text>
sections. Such as:
If a an inheriting template overrides <%text filter='h'>class_%text>
to be <%text filter='h'>white%text>
, as in:
You'll get output like:
<%call expr="formatting.code()"><%text>