Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 5d5a53cfb1fc82fdbf1e3e9ea3253fa5 > files > 91

python-mako-0.2.4-1mdv2010.0.noarch.rpm

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Mako Documentation - Inheritance</title>
	
    <link rel="stylesheet" href="docs.css"></link>
    <link rel="stylesheet" href="highlight.css"></link>
    




</head>
<body>










<div id="topanchor"><a name="top">&nbsp;</a></div>

<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>

<h1>Mako Documentation</h1>

<div class="versionheader">Version: 0.2.4   Last Updated: 12/22/08 22:46:37</div>







<A name=""></a>


    <div class="topnav">

    
    <div class="toolbar">
        <div class="prevnext">
            Previous: <a href="namespaces.html">Namespaces</a>

            |
            Next: <a href="filtering.html">Filtering and Buffering</a>
        </div>
        <h3><a href="index.html">Table of Contents</a></h3>
    </div>


    <br/>
	<a href="#inheritance">Inheritance</a>
	
	
    <ul>
        <li><A style="" href="inheritance.html#inheritance_next">Using the "next" namespace to produce content wrapping</a></li>
        
            
    <ul>
    </ul>

        <li><A style="" href="inheritance.html#inheritance_parent">Using the "parent" namespace to augment defs</a></li>
        
            
    <ul>
    </ul>

        <li><A style="" href="inheritance.html#inheritance_inheritable">Inheritable Attributes</a></li>
        
            
    <ul>
    </ul>

    </ul>

	</div>











    
    <A name="inheritance"></a>
    
    <div class="section">
    

    <h3>Inheritance</h3>
    
    

<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 <code>A</code> inherits from template <code>B</code>, then template <code>A</code> agrees to send the executional control to template <code>B</code> at runtime (<code>A</code> is called the <strong>inheriting</strong> template).  Template <code>B</code>, the <strong>inherited</strong> template, then makes decisions as to what resources from <code>A</code> shall be executed.
</p>
<p>In practice, it looks like this.  Heres a hypothetical inheriting template, <code>index.html</code>:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## index.html</span>
<span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;base.html&quot;</span><span class="cp">/&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;header()&quot;</span><span class="cp">&gt;</span>
    this is some header content
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

this is the body content.
</pre></div>

    </div>
<p>And <code>base.html</code>, the inherited template:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## base.html</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</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="nt">&lt;/div&gt;</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="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</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="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;footer()&quot;</span><span class="cp">&gt;</span>
    this is the footer
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
</pre></div>

    </div>
<p>Here is a breakdown of the execution:
</p>
<ul>
 <li><p>When <code>index.html</code> is rendered, control immediately passes to <code>base.html</code>.
</p>

 </li>

 <li><p><code>base.html</code> then renders the top part of an HTML document, then calls the method <code>header()</code> off of a built in namespace called <code>self</code> (this namespace was first introduced in the Namespaces chapter in <a href="namespaces.html#namespaces_builtin_self">self</a>).  Since <code>index.html</code> is the topmost template and also defines a def called <code>header()</code>, its this <code>header()</code> def that gets executed.
</p>

 </li>

 <li><p>Control comes back to <code>base.html</code>.  Some more HTML is rendered.
</p>

 </li>

 <li><p><code>base.html</code> executes <code>self.body()</code>.  The <code>body()</code> function on all template-based namespaces refers to the main body of the template, therefore the main body of <code>index.html</code> is rendered.
</p>

 </li>

 <li><p>Control comes back to <code>base.html</code>.  More HTML is rendered, then the <code>self.footer()</code> expression is invoked.
</p>

 </li>

 <li><p>The <code>footer</code> def is only defined in <code>base.html</code>, so being the topmost definition of <code>footer</code>, its the one that executes.  If <code>index.html</code> also specified <code>footer</code>, then its version would <strong>override</strong> that of the base.
</p>

 </li>

 <li><p><code>base.html</code> finishes up rendering its HTML and the template is complete, producing:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</span>
            this is some header content
        <span class="nt">&lt;/div&gt;</span>

        this is the body content.

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</span>
            this is the footer
        <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>

    </div>

 </li>
</ul>
<p>...and that is template inheritance in a nutshell.  The main idea is that the methods that you call upon <code>self</code> always correspond to the topmost definition of that method.  Very much the way <code>self</code> 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>


    
    <A name="inheritance_next"></a>
    
    <div class="subsection">
    

    <h3>Using the "next" namespace to produce content wrapping</h3>
    
    

<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 <code>self.body()</code> 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 <code>next</code>, which is the namespace of the template <strong>immediately following</strong> the current template.
</p>
<p>Lets change the line in <code>base.html</code> which calls upon <code>self.body()</code> to instead call upon <code>next.body()</code>:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## base.html</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</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="nt">&lt;/div&gt;</span>

        <span class="cp">${</span><span class="n">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</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="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;footer()&quot;</span><span class="cp">&gt;</span>
    this is the footer
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
</pre></div>

    </div>
<p>Lets also add an intermediate template called <code>layout.html</code>, which inherits from <code>base.html</code>:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## layout.html</span>
<span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;base.html&quot;</span><span class="cp">/&gt;</span>
<span class="nt">&lt;ul&gt;</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="nt">&lt;/ul&gt;</span>
<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;mainlayout&quot;</span><span class="nt">&gt;</span>
    <span class="cp">${</span><span class="n">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>
<span class="nt">&lt;/div&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;toolbar()&quot;</span><span class="cp">&gt;</span>
    <span class="nt">&lt;li&gt;</span>selection 1<span class="nt">&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;</span>selection 2<span class="nt">&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;</span>selection 3<span class="nt">&lt;/li&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
</pre></div>

    </div>
<p>And finally change <code>index.html</code> to inherit from <code>layout.html</code> instead:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## index.html</span>
<span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;layout.html&quot;</span><span class="cp">/&gt;</span>

<span class="cp">## .. rest of template</span>
</pre></div>

    </div>
<p>In this setup, each call to <code>next.body()</code> will render the body of the next template in the inheritance chain (which can be written as <code>base.html -&gt; layout.html -&gt; index.html</code>).  Control is still first passed to the bottommost template <code>base.html</code>, and <code>self</code> still references the topmost definition of any particular def.
</p>
<p>The output we get would be:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</span>
            this is some header content
        <span class="nt">&lt;/div&gt;</span>

        <span class="nt">&lt;ul&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 1<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 2<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 3<span class="nt">&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;mainlayout&quot;</span><span class="nt">&gt;</span>
        this is the body content.
        <span class="nt">&lt;/div&gt;</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</span>
            this is the footer
        <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>

    </div>
<p>So above, we have the <code>&lt;html&gt;</code>, <code>&lt;body&gt;</code> and <code>header</code>/<code>footer</code> layout of <code>base.html</code>, we have the <code>&lt;ul&gt;</code> and <code>mainlayout</code> section of <code>layout.html</code>, and the main body of <code>index.html</code> as well as its overridden <code>header</code> def.  The <code>layout.html</code> template is inserted into the middle of the chain without <code>base.html</code> having to change anything.  Without the <code>next</code> namespace, only the main body of <code>index.html</code> could be used; there would be no way to call <code>layout.html</code>'s body content.
</p>



            <a href="#top">back to section top</a>
    </div>



    
    <A name="inheritance_parent"></a>
    
    <div class="subsection">
    

    <h3>Using the "parent" namespace to augment defs</h3>
    
    

<p>Lets now look at the other inheritance-specific namespace, the opposite of <code>next</code> called <code>parent</code>.  <code>parent</code> 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 <code>super</code> keyword in Python.  Lets modify <code>index.html</code> to augment the list of selections provided by the <code>toolbar</code> function in <code>layout.html</code>:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">## index.html</span>
<span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;layout.html&quot;</span><span class="cp">/&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;header()&quot;</span><span class="cp">&gt;</span>
    this is some header content
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;toolbar()&quot;</span><span class="cp">&gt;</span>
    <span class="cp">## call the parent&#39;s toolbar first</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="nt">&lt;li&gt;</span>selection 4<span class="nt">&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;</span>selection 5<span class="nt">&lt;/li&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

this is the body content.
</pre></div>

    </div>
<p>Above, we implemented a <code>toolbar()</code> function, which is meant to override the definition of <code>toolbar</code> within the inherited template <code>layout.html</code>.  However, since we want the content from that of <code>layout.html</code> as well, we call it via the <code>parent</code> 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="code">
        <div class="highlight"><pre><span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;header&quot;</span><span class="nt">&gt;</span>
            this is some header content
        <span class="nt">&lt;/div&gt;</span>

        <span class="nt">&lt;ul&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 1<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 2<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 3<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 4<span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span>selection 5<span class="nt">&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;mainlayout&quot;</span><span class="nt">&gt;</span>
        this is the body content.
        <span class="nt">&lt;/div&gt;</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</span>
            this is the footer
        <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>

    </div>
<p>and you're now a template inheritance ninja !
</p>



            <a href="#top">back to section top</a>
    </div>



    
    <A name="inheritance_inheritable"></a>
    
    <div class="subsection">
    

    <h3>Inheritable Attributes</h3>
    
    

<p>The <code>attr</code> accessor of the <code>Namespace</code> object allows access to module level variables declared in a template.  By accessing <code>self.attr</code>, you can access regular attributes from the inheritance chain as declared in <code>&lt;%! %&gt;</code> sections.  Such as:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</span>
    <span class="n">class_</span> <span class="o">=</span> <span class="s">&quot;grey&quot;</span>
<span class="cp">%&gt;</span>

<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;</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="s">&quot;</span><span class="nt">&gt;</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="nt">&lt;/div&gt;</span>
</pre></div>

    </div>
<p>If a an inheriting template overrides <code>class_</code> to be <code>white</code>, as in:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</span>
    <span class="n">class_</span> <span class="o">=</span> <span class="s">&quot;white&quot;</span>
<span class="cp">%&gt;</span>
<span class="cp">&lt;%</span><span class="nb">inherit</span> <span class="na">file=</span><span class="s">&quot;parent.html&quot;</span><span class="cp">/&gt;</span>

This is the body
</pre></div>

    </div>
<p>You'll get output like:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;white&quot;</span><span class="nt">&gt;</span>
    This is the body
<span class="nt">&lt;/div&gt;</span>
</pre></div>

    </div>




    </div>




            <a href="#top">back to section top</a>
    </div>


</html>


    <div class="toolbar">
        <div class="prevnext">
            Previous: <a href="namespaces.html">Namespaces</a>

            |
            Next: <a href="filtering.html">Filtering and Buffering</a>
        </div>
        <h3><a href="index.html">Table of Contents</a></h3>
    </div>






</body>
</html>