~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/doc/build/content/inheritance.txt

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Inheritance
 
2
===================
 
3
 
 
4
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 `A` inherits from template `B`, then template `A` agrees to send the executional control to template `B` at runtime (`A` is called the **inheriting** template).  Template `B`, the **inherited** template, then makes decisions as to what resources from `A` shall be executed.
 
5
 
 
6
In practice, it looks like this.  Heres a hypothetical inheriting template, `index.html`:
 
7
 
 
8
    ## index.html
 
9
    <%inherit file="base.html"/>
 
10
    
 
11
    <%def name="header()">
 
12
        this is some header content
 
13
    </%def>
 
14
    
 
15
    this is the body content.
 
16
    
 
17
And `base.html`, the inherited template:
 
18
 
 
19
    ## base.html
 
20
    <html>
 
21
        <body>
 
22
            <div class="header">
 
23
                ${self.header()}
 
24
            </div>
 
25
            
 
26
            ${self.body()}
 
27
            
 
28
            <div class="footer">
 
29
                ${self.footer()}
 
30
            </div>
 
31
        </body>
 
32
    </html>
 
33
 
 
34
    <%def name="footer()">
 
35
        this is the footer
 
36
    </%def>
 
37
 
 
38
Here is a breakdown of the execution:
 
39
    
 
40
* When `index.html` is rendered, control immediately passes to `base.html`.
 
41
* `base.html` then renders the top part of an HTML document, then calls the method `header()` off of a built in namespace called `self` (this namespace was first introduced in the Namespaces chapter in [namespaces_builtin_self](rel:namespaces_builtin_self)).  Since `index.html` is the topmost template and also defines a def called `header()`, its this `header()` def that gets executed.
 
42
* Control comes back to `base.html`.  Some more HTML is rendered.
 
43
* `base.html` executes `self.body()`.  The `body()` function on all template-based namespaces refers to the main body of the template, therefore the main body of `index.html` is rendered.
 
44
* Control comes back to `base.html`.  More HTML is rendered, then the `self.footer()` expression is invoked.
 
45
* The `footer` def is only defined in `base.html`, so being the topmost definition of `footer`, its the one that executes.  If `index.html` also specified `footer`, then its version would **override** that of the base.
 
46
* `base.html` finishes up rendering its HTML and the template is complete, producing:
 
47
 
 
48
        <html>
 
49
            <body>
 
50
                <div class="header">
 
51
                    this is some header content
 
52
                </div>
 
53
        
 
54
                this is the body content.
 
55
            
 
56
                <div class="footer">
 
57
                    this is the footer
 
58
                </div>
 
59
            </body>
 
60
        </html>
 
61
 
 
62
...and that is template inheritance in a nutshell.  The main idea is that the methods that you call upon `self` always correspond to the topmost definition of that method.  Very much the way `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).
 
63
 
 
64
### Using the "next" namespace to produce content wrapping {@name=next}
 
65
 
 
66
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 `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 `next`, which is the namespace of the template **immediately following** the current template.
 
67
 
 
68
Lets change the line in `base.html` which calls upon `self.body()` to instead call upon `next.body()`:
 
69
 
 
70
    ## base.html
 
71
    <html>
 
72
        <body>
 
73
            <div class="header">
 
74
                ${self.header()}
 
75
            </div>
 
76
        
 
77
            ${next.body()}
 
78
        
 
79
            <div class="footer">
 
80
                ${self.footer()}
 
81
            </div>
 
82
        </body>
 
83
    </html>
 
84
 
 
85
    <%def name="footer()">
 
86
        this is the footer
 
87
    </%def>
 
88
 
 
89
Lets also add an intermediate template called `layout.html`, which inherits from `base.html`:
 
90
 
 
91
    ## layout.html
 
92
    <%inherit file="base.html"/>
 
93
    <ul>
 
94
        ${self.toolbar()}
 
95
    </ul>
 
96
    <div class="mainlayout">
 
97
        ${next.body()}
 
98
    </div>
 
99
    
 
100
    <%def name="toolbar">
 
101
        <li>selection 1</li>
 
102
        <li>selection 2</li>
 
103
        <li>selection 3</li>
 
104
    </%def>    
 
105
 
 
106
And finally change `index.html` to inherit from `layout.html` instead:
 
107
 
 
108
    ## index.html
 
109
    <%inherit file="layout.html"/>
 
110
    
 
111
    ## .. rest of template
 
112
    
 
113
In this setup, each call to `next.body()` will render the body of the next template in the inheritance chain (which can be written as `base.html -> layout.html -> index.html`).  Control is still first passed to the bottommost template `base.html`, and `self` still references the topmost definition of any particular def.
 
114
 
 
115
The output we get would be:
 
116
 
 
117
    <html>
 
118
        <body>
 
119
            <div class="header">
 
120
                this is some header content
 
121
            </div>
 
122
 
 
123
            <ul>
 
124
                <li>selection 1</li>
 
125
                <li>selection 2</li>
 
126
                <li>selection 3</li>
 
127
            </ul>
 
128
            
 
129
            <div class="mainlayout">
 
130
            this is the body content.
 
131
            </div>
 
132
            
 
133
            <div class="footer">
 
134
                this is the footer
 
135
            </div>
 
136
        </body>
 
137
    </html>
 
138
 
 
139
So above, we have the `<html>`, `<body>` and `header`/`footer` layout of `base.html`, we have the `<ul>` and `mainlayout` section of `layout.html`, and the main body of `index.html` as well as its overridden `header` def.  The `layout.html` template is inserted into the middle of the chain without `base.html` having to change anything.  Without the `next` namespace, only the main body of `index.html` could be used; there would be no way to call `layout.html`'s body content.
 
140
 
 
141
### Using the "parent" namespace to augment defs {@name=parent}
 
142
 
 
143
Lets now look at the other inheritance-specific namespace, the opposite of `next` called `parent`.  `parent` 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 `super` keyword in Python.  Lets modify `index.html` to augment the list of selections provided by the `toolbar` function in `layout.html`:
 
144
 
 
145
    ## index.html
 
146
    <%inherit file="layout.html"/>
 
147
 
 
148
    <%def name="header()">
 
149
        this is some header content
 
150
    </%def>
 
151
 
 
152
    <%def name="toolbar()">
 
153
        ## call the parent's toolbar first
 
154
        ${parent.toolbar()}
 
155
        <li>selection 4</li>
 
156
        <li>selection 5</li>
 
157
    </%def>
 
158
        
 
159
    this is the body content.
 
160
 
 
161
Above, we implemented a `toolbar()` function, which is meant to override the definition of `toolbar` within the inherited template `layout.html`.  However, since we want the content from that of `layout.html` as well, we call it via the `parent` 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:
 
162
 
 
163
    <html>
 
164
        <body>
 
165
            <div class="header">
 
166
                this is some header content
 
167
            </div>
 
168
 
 
169
            <ul>
 
170
                <li>selection 1</li>
 
171
                <li>selection 2</li>
 
172
                <li>selection 3</li>
 
173
                <li>selection 4</li>
 
174
                <li>selection 5</li>
 
175
            </ul>
 
176
        
 
177
            <div class="mainlayout">
 
178
            this is the body content.
 
179
            </div>
 
180
        
 
181
            <div class="footer">
 
182
                this is the footer
 
183
            </div>
 
184
        </body>
 
185
    </html>
 
186
 
 
187
and you're now a template inheritance ninja !