# -*- 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 <%! 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 inherits from template <%text filter='h'>B, then template <%text filter='h'>A agrees to send the executional control to template <%text filter='h'>B at runtime (<%text filter='h'>A is called the inheriting template). Template <%text filter='h'>B, the inherited template, then makes decisions as to what resources from <%text filter='h'>A shall be executed.

In practice, it looks like this. Heres a hypothetical inheriting template, <%text filter='h'>index.html:

<%call expr="formatting.code()"><%text>## index.html <%inherit file="base.html"/> <%def name="header()"> this is some header content this is the body content.

And <%text filter='h'>base.html, the inherited template:

<%call expr="formatting.code()"><%text>## base.html
${self.header()}
${self.body()} <%def name="footer()"> this is the footer

Here is a breakdown of the execution:

...and that is template inheritance in a nutshell. The main idea is that the methods that you call upon <%text filter='h'>self always correspond to the topmost definition of that method. Very much the way <%text filter='h'>self 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).

<%call expr="formatting.section(path='inheritance_next',paged=paged,extension=extension,toc=toc)">

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() 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, which is the namespace of the template immediately following the current template.

Lets change the line in <%text filter='h'>base.html which calls upon <%text filter='h'>self.body() to instead call upon <%text filter='h'>next.body():

<%call expr="formatting.code()"><%text>## base.html
${self.header()}
${next.body()} <%def name="footer()"> this is the footer

Lets also add an intermediate template called <%text filter='h'>layout.html, which inherits from <%text filter='h'>base.html:

<%call expr="formatting.code()"><%text>## layout.html <%inherit file="base.html"/>
${next.body()}
<%def name="toolbar()">
  • selection 1
  • selection 2
  • selection 3
  • And finally change <%text filter='h'>index.html to inherit from <%text filter='h'>layout.html instead:

    <%call expr="formatting.code()"><%text>## index.html <%inherit file="layout.html"/> ## .. rest of template

    In this setup, each call to <%text filter='h'>next.body() 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). Control is still first passed to the bottommost template <%text filter='h'>base.html, and <%text filter='h'>self still references the topmost definition of any particular def.

    The output we get would be:

    <%call expr="formatting.code()"><%text>
    this is some header content
    this is the body content.

    So above, we have the <%text filter='h'>, <%text filter='h'> and <%text filter='h'>header/<%text filter='h'>footer layout of <%text filter='h'>base.html, we have the <%text filter='h'>