<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Inheritance — Mako 0.3.6 Documentation</title> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="_static/docs.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '#', VERSION: '0.3.6', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html' }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/init.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="top" title="Mako 0.3.6 Documentation" href="index.html" /> <link rel="next" title="Filtering and Buffering" href="filtering.html" /> <link rel="prev" title="Namespaces" href="namespaces.html" /> </head> <body> <h1>Mako 0.3.6 Documentation</h1> <div id="search"> Search: <form class="search" action="search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <div class="versionheader"> Version: <span class="versionnum">0.3.6</span> Last Updated: 11/13/2010 16:04:30 </div> <div class="clearboth"></div> <div class="topnav"> <div id="pagecontrol"> <a href="genindex.html">Index</a> <div class="sourcelink">(<a href="_sources/inheritance.txt">view source)</div> </div> <div class="navbanner"> <a class="totoc" href="index.html">Table of Contents</a> » Inheritance <div class="prevnext"> Previous: <a href="namespaces.html" title="previous chapter">Namespaces</a> Next: <a href="filtering.html" title="next chapter">Filtering and Buffering</a> </div> <h2> Inheritance </h2> </div> <ul> <li><a class="reference internal" href="#">Inheritance</a><ul> <li><a class="reference internal" href="#using-the-next-namespace-to-produce-content-wrapping">Using the “next” namespace to produce content wrapping</a></li> <li><a class="reference internal" href="#using-the-parent-namespace-to-augment-defs">Using the “parent” namespace to augment defs</a></li> <li><a class="reference internal" href="#inheritable-attributes">Inheritable Attributes</a></li> </ul> </li> </ul> <div class="clearboth"></div> </div> <div class="document"> <div class="body"> <div class="section" id="inheritance"> <span id="inheritance-toplevel"></span><h1>Inheritance<a class="headerlink" href="#inheritance" title="Permalink to this headline">¶</a></h1> <p>Using template inheritance, two or more templates can organize themselves into an <strong>inheritance chain</strong>, where content and functions from all involved templates can be intermixed. The general paradigm of template inheritance is this: if a template <tt class="docutils literal"><span class="pre">A</span></tt> inherits from template <tt class="docutils literal"><span class="pre">B</span></tt>, then template <tt class="docutils literal"><span class="pre">A</span></tt> agrees to send the executional control to template <tt class="docutils literal"><span class="pre">B</span></tt> at runtime (<tt class="docutils literal"><span class="pre">A</span></tt> is called the <strong>inheriting</strong> template). Template <tt class="docutils literal"><span class="pre">B</span></tt>, the <strong>inherited</strong> template, then makes decisions as to what resources from <tt class="docutils literal"><span class="pre">A</span></tt> shall be executed.</p> <p>In practice, it looks like this. Heres a hypothetical inheriting template, <tt class="docutils literal"><span class="pre">index.html</span></tt>:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## index.html</span><span class="x"></span> <span class="cp"><%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">"base.html"</span><span class="cp">/></span><span class="x"></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"header()"</span><span class="cp">></span><span class="x"></span> <span class="x"> this is some header content</span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> <span class="x">this is the body content.</span> </pre></div> </div> <p>And <tt class="docutils literal"><span class="pre">base.html</span></tt>, the inherited template:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.html</span><span class="x"></span> <span class="x"><html></span> <span class="x"> <body></span> <span class="x"> <div class="header"></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> </div></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> <div class="footer"></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">footer</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> </div></span> <span class="x"> </body></span> <span class="x"></html></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"footer()"</span><span class="cp">></span><span class="x"></span> <span class="x"> this is the footer</span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> </pre></div> </div> <p>Here is a breakdown of the execution:</p> <ul class="simple"> <li>When <tt class="docutils literal"><span class="pre">index.html</span></tt> is rendered, control immediately passes to <tt class="docutils literal"><span class="pre">base.html</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">base.html</span></tt> then renders the top part of an HTML document, then calls the method <tt class="docutils literal"><span class="pre">header()</span></tt> off of a built in namespace called <tt class="docutils literal"><span class="pre">self</span></tt> (this namespace was first introduced in the Namespaces chapter in <a class="reference internal" href="namespaces.html#namespace-self"><em>self</em></a>). Since <tt class="docutils literal"><span class="pre">index.html</span></tt> is the topmost template and also defines a def called <tt class="docutils literal"><span class="pre">header()</span></tt>, its this <tt class="docutils literal"><span class="pre">header()</span></tt> def that gets executed.</li> <li>Control comes back to <tt class="docutils literal"><span class="pre">base.html</span></tt>. Some more HTML is rendered.</li> <li><tt class="docutils literal"><span class="pre">base.html</span></tt> executes <tt class="docutils literal"><span class="pre">self.body()</span></tt>. The <tt class="docutils literal"><span class="pre">body()</span></tt> function on all template-based namespaces refers to the main body of the template, therefore the main body of <tt class="docutils literal"><span class="pre">index.html</span></tt> is rendered.</li> <li>Control comes back to <tt class="docutils literal"><span class="pre">base.html</span></tt>. More HTML is rendered, then the <tt class="docutils literal"><span class="pre">self.footer()</span></tt> expression is invoked.</li> <li>The <tt class="docutils literal"><span class="pre">footer</span></tt> def is only defined in <tt class="docutils literal"><span class="pre">base.html</span></tt>, so being the topmost definition of <tt class="docutils literal"><span class="pre">footer</span></tt>, its the one that executes. If <tt class="docutils literal"><span class="pre">index.html</span></tt> also specified <tt class="docutils literal"><span class="pre">footer</span></tt>, then its version would <strong>override</strong> that of the base.</li> <li><tt class="docutils literal"><span class="pre">base.html</span></tt> finishes up rendering its HTML and the template is complete, producing:</li> </ul> <div class="highlight-html"><div class="highlight"><pre><span class="nt"><html></span> <span class="nt"><body></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"header"</span><span class="nt">></span> this is some header content <span class="nt"></div></span> this is the body content. <span class="nt"><div</span> <span class="na">class=</span><span class="s">"footer"</span><span class="nt">></span> this is the footer <span class="nt"></div></span> <span class="nt"></body></span> <span class="nt"></html></span> </pre></div> </div> <p>...and that is template inheritance in a nutshell. The main idea is that the methods that you call upon <tt class="docutils literal"><span class="pre">self</span></tt> always correspond to the topmost definition of that method. Very much the way <tt class="docutils literal"><span class="pre">self</span></tt> 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).</p> <div class="section" id="using-the-next-namespace-to-produce-content-wrapping"> <h2>Using the “next” namespace to produce content wrapping<a class="headerlink" href="#using-the-next-namespace-to-produce-content-wrapping" title="Permalink to this headline">¶</a></h2> <p>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 <tt class="docutils literal"><span class="pre">self.body()</span></tt> to get at the inheriting template’s body, since that is only the topmost body. To get at the body of the <em>next</em> template, you call upon the namespace <tt class="docutils literal"><span class="pre">next</span></tt>, which is the namespace of the template <strong>immediately following</strong> the current template.</p> <p>Lets change the line in <tt class="docutils literal"><span class="pre">base.html</span></tt> which calls upon <tt class="docutils literal"><span class="pre">self.body()</span></tt> to instead call upon <tt class="docutils literal"><span class="pre">next.body()</span></tt>:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## base.html</span><span class="x"></span> <span class="x"><html></span> <span class="x"> <body></span> <span class="x"> <div class="header"></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> </div></span> <span class="x"> </span><span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> <div class="footer"></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">footer</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> </div></span> <span class="x"> </body></span> <span class="x"></html></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"footer()"</span><span class="cp">></span><span class="x"></span> <span class="x"> this is the footer</span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> </pre></div> </div> <p>Lets also add an intermediate template called <tt class="docutils literal"><span class="pre">layout.html</span></tt>, which inherits from <tt class="docutils literal"><span class="pre">base.html</span></tt>:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## layout.html</span><span class="x"></span> <span class="cp"><%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">"base.html"</span><span class="cp">/></span><span class="x"></span> <span class="x"><ul></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">toolbar</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"></ul></span> <span class="x"><div class="mainlayout"></span> <span class="x"> </span><span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"></div></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"toolbar()"</span><span class="cp">></span><span class="x"></span> <span class="x"> <li>selection 1</li></span> <span class="x"> <li>selection 2</li></span> <span class="x"> <li>selection 3</li></span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> </pre></div> </div> <p>And finally change <tt class="docutils literal"><span class="pre">index.html</span></tt> to inherit from <tt class="docutils literal"><span class="pre">layout.html</span></tt> instead:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## index.html</span><span class="x"></span> <span class="cp"><%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">"layout.html"</span><span class="cp">/></span> <span class="cp">## .. rest of template</span><span class="x"></span> </pre></div> </div> <p>In this setup, each call to <tt class="docutils literal"><span class="pre">next.body()</span></tt> will render the body of the next template in the inheritance chain (which can be written as <tt class="docutils literal"><span class="pre">base.html</span> <span class="pre">-></span> <span class="pre">layout.html</span> <span class="pre">-></span> <span class="pre">index.html</span></tt>). Control is still first passed to the bottommost template <tt class="docutils literal"><span class="pre">base.html</span></tt>, and <tt class="docutils literal"><span class="pre">self</span></tt> still references the topmost definition of any particular def.</p> <p>The output we get would be:</p> <div class="highlight-html"><div class="highlight"><pre><span class="nt"><html></span> <span class="nt"><body></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"header"</span><span class="nt">></span> this is some header content <span class="nt"></div></span> <span class="nt"><ul></span> <span class="nt"><li></span>selection 1<span class="nt"></li></span> <span class="nt"><li></span>selection 2<span class="nt"></li></span> <span class="nt"><li></span>selection 3<span class="nt"></li></span> <span class="nt"></ul></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"mainlayout"</span><span class="nt">></span> this is the body content. <span class="nt"></div></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"footer"</span><span class="nt">></span> this is the footer <span class="nt"></div></span> <span class="nt"></body></span> <span class="nt"></html></span> </pre></div> </div> <p>So above, we have the <tt class="docutils literal"><span class="pre"><html></span></tt>, <tt class="docutils literal"><span class="pre"><body></span></tt> and <tt class="docutils literal"><span class="pre">header</span></tt>/<tt class="docutils literal"><span class="pre">footer</span></tt> layout of <tt class="docutils literal"><span class="pre">base.html</span></tt>, we have the <tt class="docutils literal"><span class="pre"><ul></span></tt> and <tt class="docutils literal"><span class="pre">mainlayout</span></tt> section of <tt class="docutils literal"><span class="pre">layout.html</span></tt>, and the main body of <tt class="docutils literal"><span class="pre">index.html</span></tt> as well as its overridden <tt class="docutils literal"><span class="pre">header</span></tt> def. The <tt class="docutils literal"><span class="pre">layout.html</span></tt> template is inserted into the middle of the chain without <tt class="docutils literal"><span class="pre">base.html</span></tt> having to change anything. Without the <tt class="docutils literal"><span class="pre">next</span></tt> namespace, only the main body of <tt class="docutils literal"><span class="pre">index.html</span></tt> could be used; there would be no way to call <tt class="docutils literal"><span class="pre">layout.html</span></tt>‘s body content.</p> </div> <div class="section" id="using-the-parent-namespace-to-augment-defs"> <h2>Using the “parent” namespace to augment defs<a class="headerlink" href="#using-the-parent-namespace-to-augment-defs" title="Permalink to this headline">¶</a></h2> <p>Lets now look at the other inheritance-specific namespace, the opposite of <tt class="docutils literal"><span class="pre">next</span></tt> called <tt class="docutils literal"><span class="pre">parent</span></tt>. <tt class="docutils literal"><span class="pre">parent</span></tt> is the namespace of the template <strong>immediately preceding</strong> 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 <tt class="docutils literal"><span class="pre">super</span></tt> keyword in Python. Lets modify <tt class="docutils literal"><span class="pre">index.html</span></tt> to augment the list of selections provided by the <tt class="docutils literal"><span class="pre">toolbar</span></tt> function in <tt class="docutils literal"><span class="pre">layout.html</span></tt>:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp">## index.html</span><span class="x"></span> <span class="cp"><%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">"layout.html"</span><span class="cp">/></span><span class="x"></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"header()"</span><span class="cp">></span><span class="x"></span> <span class="x"> this is some header content</span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> <span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"toolbar()"</span><span class="cp">></span> <span class="cp">## call the parent's toolbar first</span><span class="x"></span> <span class="x"> </span><span class="cp">${</span><span class="n">parent</span><span class="o">.</span><span class="n">toolbar</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"> <li>selection 4</li></span> <span class="x"> <li>selection 5</li></span> <span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span> <span class="x">this is the body content.</span> </pre></div> </div> <p>Above, we implemented a <tt class="docutils literal"><span class="pre">toolbar()</span></tt> function, which is meant to override the definition of <tt class="docutils literal"><span class="pre">toolbar</span></tt> within the inherited template <tt class="docutils literal"><span class="pre">layout.html</span></tt>. However, since we want the content from that of <tt class="docutils literal"><span class="pre">layout.html</span></tt> as well, we call it via the <tt class="docutils literal"><span class="pre">parent</span></tt> 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:</p> <div class="highlight-html"><div class="highlight"><pre><span class="nt"><html></span> <span class="nt"><body></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"header"</span><span class="nt">></span> this is some header content <span class="nt"></div></span> <span class="nt"><ul></span> <span class="nt"><li></span>selection 1<span class="nt"></li></span> <span class="nt"><li></span>selection 2<span class="nt"></li></span> <span class="nt"><li></span>selection 3<span class="nt"></li></span> <span class="nt"><li></span>selection 4<span class="nt"></li></span> <span class="nt"><li></span>selection 5<span class="nt"></li></span> <span class="nt"></ul></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"mainlayout"</span><span class="nt">></span> this is the body content. <span class="nt"></div></span> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"footer"</span><span class="nt">></span> this is the footer <span class="nt"></div></span> <span class="nt"></body></span> <span class="nt"></html></span> </pre></div> </div> <p>and you’re now a template inheritance ninja !</p> </div> <div class="section" id="inheritable-attributes"> <h2>Inheritable Attributes<a class="headerlink" href="#inheritable-attributes" title="Permalink to this headline">¶</a></h2> <p>The <tt class="docutils literal"><span class="pre">attr</span></tt> accessor of the <a class="reference internal" href="namespaces.html#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a> object allows access to module level variables declared in a template. By accessing <tt class="docutils literal"><span class="pre">self.attr</span></tt>, you can access regular attributes from the inheritance chain as declared in <tt class="docutils literal"><span class="pre"><%!</span> <span class="pre">%></span></tt> sections. Such as:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span> <span class="n">class_</span> <span class="o">=</span> <span class="s">"grey"</span> <span class="cp">%></span><span class="x"></span> <span class="x"><div class="</span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">attr</span><span class="o">.</span><span class="n">class_</span><span class="cp">}</span><span class="x">"></span> <span class="x"> </span><span class="cp">${</span><span class="bp">self</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span><span class="x"></span> <span class="x"></div></span> </pre></div> </div> <p>If a an inheriting template overrides <tt class="docutils literal"><span class="pre">class_</span></tt> to be <tt class="docutils literal"><span class="pre">white</span></tt>, as in:</p> <div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span> <span class="n">class_</span> <span class="o">=</span> <span class="s">"white"</span> <span class="cp">%></span><span class="x"></span> <span class="cp"><%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">"parent.html"</span><span class="cp">/></span><span class="x"></span> <span class="x">This is the body</span> </pre></div> </div> <p>You’ll get output like:</p> <div class="highlight-html"><div class="highlight"><pre><span class="nt"><div</span> <span class="na">class=</span><span class="s">"white"</span><span class="nt">></span> This is the body <span class="nt"></div></span> </pre></div> </div> </div> </div> </div> </div> <div class="bottomnav"> <div class="prevnext"> Previous: <a href="namespaces.html" title="previous chapter">Namespaces</a> Next: <a href="filtering.html" title="next chapter">Filtering and Buffering</a> </div> <div class="doc_copyright"> © Copyright the Mako authors and contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1. </div> </div> </body> </html>