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

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/doc/usage.html

  • 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
<html>
 
2
<head>
 
3
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 
4
        <title>Mako Documentation - Basic Usage</title>
 
5
        
 
6
    <link rel="stylesheet" href="docs.css"></link>
 
7
    <link rel="stylesheet" href="highlight.css"></link>
 
8
    
 
9
 
 
10
 
 
11
 
 
12
 
 
13
</head>
 
14
<body>
 
15
 
 
16
 
 
17
 
 
18
 
 
19
 
 
20
 
 
21
 
 
22
 
 
23
 
 
24
 
 
25
<div id="topanchor"><a name="top">&nbsp;</a></div>
 
26
 
 
27
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
 
28
 
 
29
<h1>Mako Documentation</h1>
 
30
 
 
31
<div class="versionheader">Version: 0.1.9   Last Updated: 09/25/07 16:19:18</div>
 
32
 
 
33
 
 
34
 
 
35
 
 
36
 
 
37
 
 
38
 
 
39
<A name=""></a>
 
40
 
 
41
 
 
42
    <div class="topnav">
 
43
 
 
44
    
 
45
    <div class="toolbar">
 
46
        <div class="prevnext">
 
47
 
 
48
            
 
49
            Next: <a href="syntax.html">Syntax</a>
 
50
        </div>
 
51
        <h3><a href="index.html">Table of Contents</a></h3>
 
52
    </div>
 
53
 
 
54
 
 
55
    <br/>
 
56
        <a href="#usage">Basic Usage</a>
 
57
        
 
58
        
 
59
    <ul>
 
60
        <li><A style="" href="usage.html#usage_file">Using File-Based Templates</a></li>
 
61
        
 
62
            
 
63
    <ul>
 
64
    </ul>
 
65
 
 
66
        <li><A style="" href="usage.html#usage_lookup">Using TemplateLookup</a></li>
 
67
        
 
68
            
 
69
    <ul>
 
70
        <li><A style="" href="usage.html#usage_lookup_size">Setting the Collection Size</a></li>
 
71
        
 
72
            
 
73
    <ul>
 
74
    </ul>
 
75
 
 
76
        <li><A style="" href="usage.html#usage_lookup_checks">Setting Filesystem Checks</a></li>
 
77
        
 
78
            
 
79
    <ul>
 
80
    </ul>
 
81
 
 
82
    </ul>
 
83
 
 
84
        <li><A style="" href="usage.html#usage_using">Using Unicode and Encoding</a></li>
 
85
        
 
86
            
 
87
    <ul>
 
88
    </ul>
 
89
 
 
90
        <li><A style="" href="usage.html#usage_handling">Handling Exceptions</a></li>
 
91
        
 
92
            
 
93
    <ul>
 
94
    </ul>
 
95
 
 
96
        <li><A style="" href="usage.html#usage_common">Common Framework Integrations</a></li>
 
97
        
 
98
            
 
99
    <ul>
 
100
        <li><A style="" href="usage.html#usage_common_turbogears/pylons">Turbogears/Pylons Plugin</a></li>
 
101
        
 
102
            
 
103
    <ul>
 
104
    </ul>
 
105
 
 
106
        <li><A style="" href="usage.html#usage_common_wsgi">WSGI</a></li>
 
107
        
 
108
            
 
109
    <ul>
 
110
    </ul>
 
111
 
 
112
        <li><A style="" href="usage.html#usage_common_pygments">Pygments</a></li>
 
113
        
 
114
            
 
115
    <ul>
 
116
    </ul>
 
117
 
 
118
        <li><A style="" href="usage.html#usage_common_babel">Babel (Internationalization)</a></li>
 
119
        
 
120
            
 
121
    <ul>
 
122
    </ul>
 
123
 
 
124
    </ul>
 
125
 
 
126
    </ul>
 
127
 
 
128
        </div>
 
129
 
 
130
 
 
131
 
 
132
 
 
133
 
 
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
 
 
140
    
 
141
    <A name="usage"></a>
 
142
    
 
143
    <div class="section">
 
144
    
 
145
 
 
146
    <h3>Basic Usage</h3>
 
147
    
 
148
    
 
149
 
 
150
<p>This section describes the Python API for Mako templates.  If you are using Mako within a web framework such as Pylons, the work of integrating Mako's API is already done for you, in which case you can skip to the next section, <a href="syntax.html">Syntax</a>.
 
151
</p>
 
152
<p>The most basic way to create a template and render it is through the <code>Template</code> class:
 
153
</p>
 
154
 
 
155
    
 
156
    
 
157
 
 
158
    <div class="code">
 
159
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
160
 
 
161
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello world!&quot;</span><span class="p">)</span>
 
162
<span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
163
</pre></div>
 
164
 
 
165
    </div>
 
166
<p>Above, the text argument to <code>Template</code> is <strong>compiled</strong> into a Python module representation.  This module contains a function called <code>render_body()</code>, which produces the output of the template.  When <code>mytemplate.render()</code> is called, Mako sets up a runtime environment for the template and calls the <code>render_body()</code> function, capturing the output into a buffer and returning it's string contents.
 
167
</p>
 
168
<p>The code inside the <code>render_body()</code> function has access to a namespace of variables.  You can specify these variables by sending them as additional keyword arguments to the <code>render()</code> method:
 
169
</p>
 
170
 
 
171
    
 
172
    
 
173
 
 
174
    <div class="code">
 
175
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
176
 
 
177
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello, ${name}!&quot;</span><span class="p">)</span>
 
178
<span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;jack&quot;</span><span class="p">)</span>
 
179
</pre></div>
 
180
 
 
181
    </div>
 
182
<p>The <code>template.render()</code> method calls upon Mako to create a <code>Context</code> object, which stores all the variable names accessible to the template and also stores a buffer used to capture output.  You can create this <code>Context</code> yourself and have the template render with it, using the <code>render_context</code> method:
 
183
</p>
 
184
 
 
185
    
 
186
    
 
187
 
 
188
    <div class="code">
 
189
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
190
<span class="k">from</span> <span class="nn">mako.runtime</span> <span class="k">import</span> <span class="n">Context</span>
 
191
<span class="k">from</span> <span class="nn">StringIO</span> <span class="k">import</span> <span class="n">StringIO</span>
 
192
 
 
193
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;hello, ${name}!&quot;</span><span class="p">)</span>
 
194
<span class="n">buf</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
 
195
<span class="n">ctx</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;jack&quot;</span><span class="p">)</span>
 
196
<span class="n">mytemplate</span><span class="o">.</span><span class="n">render_context</span><span class="p">(</span><span class="n">ctx</span><span class="p">)</span>
 
197
<span class="k">print</span> <span class="n">buf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
 
198
</pre></div>
 
199
 
 
200
    </div>
 
201
 
 
202
 
 
203
    
 
204
    <A name="usage_file"></a>
 
205
    
 
206
    <div class="subsection">
 
207
    
 
208
 
 
209
    <h3>Using File-Based Templates</h3>
 
210
    
 
211
    
 
212
 
 
213
<p>A <code>Template</code> can also load its template source code from a file, using the <code>filename</code> keyword argument:
 
214
</p>
 
215
 
 
216
    
 
217
    
 
218
 
 
219
    <div class="code">
 
220
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
221
 
 
222
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&#39;/docs/mytmpl.txt&#39;</span><span class="p">)</span>
 
223
<span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
224
</pre></div>
 
225
 
 
226
    </div>
 
227
<p>For improved performance, a <code>Template</code> which is loaded from a file can also cache the source code to its generated module on the filesystem as a regular Python module file (i.e. a .py file).  To do this, just add the <code>module_directory</code> argument to the template:
 
228
</p>
 
229
 
 
230
    
 
231
    
 
232
 
 
233
    <div class="code">
 
234
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
235
 
 
236
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&#39;/docs/mytmpl.txt&#39;</span><span class="p">,</span> <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">)</span>
 
237
<span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
238
</pre></div>
 
239
 
 
240
    </div>
 
241
<p>When the above code is rendered, a file <code>/tmp/mako_modules/docs/mytmpl.txt.py</code> is created containing the source code for the module.  The next time a <code>Template</code> with the same arguments is created, this module file will be automatically re-used.
 
242
</p>
 
243
 
 
244
 
 
245
 
 
246
            <a href="#top">back to section top</a>
 
247
    </div>
 
248
 
 
249
 
 
250
 
 
251
    
 
252
    <A name="usage_lookup"></a>
 
253
    
 
254
    <div class="subsection">
 
255
    
 
256
 
 
257
    <h3>Using TemplateLookup</h3>
 
258
    
 
259
    
 
260
 
 
261
<p>All of the examples thus far have dealt with the usage of a single <code>Template</code> object.  If the code within those templates tries to locate another template resource, it will need some way to find them, using simple URI strings.  For this need, the resolution of other templates from within a template is accomplished by the <code>TemplateLookup</code> class.   This class is constructed given a list of directories in which to search for templates, as well as keyword arguments that will be passed to the <code>Template</code> objects it creates.
 
262
</p>
 
263
 
 
264
    
 
265
    
 
266
 
 
267
    <div class="code">
 
268
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
269
<span class="k">from</span> <span class="nn">mako.lookup</span> <span class="k">import</span> <span class="n">TemplateLookup</span>
 
270
 
 
271
<span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">])</span>
 
272
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;&quot;&quot;&lt;</span><span class="si">%i</span><span class="s">nclude file=&quot;header.txt&quot;/&gt; hello world!&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">lookup</span><span class="o">=</span><span class="n">mylookup</span><span class="p">)</span>
 
273
</pre></div>
 
274
 
 
275
    </div>
 
276
<p>Above, we created a textual template which includes the file <code>header.txt</code>.  In order for it to have somewhere to look for <code>header.txt</code>, we passed a <code>TemplateLookup</code> object to it, which will search in the directory <code>/docs</code> for the file <code>header.txt</code>.
 
277
</p>
 
278
<p>Usually, an application will store most or all of its templates as text files on the filesystem.  So far, all of our examples have been a little bit contrived in order to illustrate the basic concepts.  But a real application would get most or all of its templates directly from the <code>TemplateLookup</code>, using the aptly named <code>get_template</code> method, which accepts the URI of the desired template:
 
279
</p>
 
280
 
 
281
    
 
282
    
 
283
 
 
284
    <div class="code">
 
285
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
286
<span class="k">from</span> <span class="nn">mako.lookup</span> <span class="k">import</span> <span class="n">TemplateLookup</span>
 
287
 
 
288
<span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span> <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">)</span>
 
289
 
 
290
<span class="k">def</span> <span class="nf">serve_template</span><span class="p">(</span><span class="n">templatename</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
 
291
    <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">mylookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">templatename</span><span class="p">)</span>
 
292
    <span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
 
293
</pre></div>
 
294
 
 
295
    </div>
 
296
<p>In the example above, we create a <code>TemplateLookup</code> which will look for templates in the <code>/docs</code> directory, and will store generated module files in the <code>/tmp/mako_modules</code> directory.  The lookup locates templates by appending the given URI to each of its search directories; so if you gave it a URI of <code>/etc/beans/info.txt</code>, it would search for the file <code>/docs/etc/beans/info.txt</code>, else raise a <code>TopLevelNotFound</code> exception, which is a custom Mako exception.
 
297
</p>
 
298
<p>When the lookup locates templates, it will also assign a <code>uri</code> property to the <code>Template</code> which is the uri passed to the <code>get_template()</code> call.  <code>Template</code> uses this uri to calculate the name of its module file.  So in the above example, a <code>templatename</code> argument of <code>/etc/beans/info.txt</code> will create a module file <code>/tmp/mako_modules/etc/beans/info.txt.py</code>.
 
299
</p>
 
300
 
 
301
 
 
302
    
 
303
    <A name="usage_lookup_size"></a>
 
304
    
 
305
    <div class="subsection">
 
306
    
 
307
 
 
308
    <h3>Setting the Collection Size</h3>
 
309
    
 
310
    
 
311
 
 
312
<p>The <code>TemplateLookup</code> also serves the important need of caching a fixed set of templates in memory at a given time, so that successive uri lookups do not result in full template compilations and/or module reloads on each request.  By default, the <code>TemplateLookup</code> size is unbounded.  You can specify a fixed size using the <code>collection_size</code> argument:
 
313
</p>
 
314
 
 
315
    
 
316
    
 
317
 
 
318
    <div class="code">
 
319
        <div class="highlight" ><pre><span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span> 
 
320
                <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/tmp/mako_modules&#39;</span><span class="p">,</span> <span class="n">collection_size</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>
 
321
</pre></div>
 
322
 
 
323
    </div>
 
324
<p>The above lookup will continue to load templates into memory until it reaches a count of around 500.  At that point, it will clean out a certain percentage of templates using a <strong>least recently used</strong> scheme.
 
325
</p>
 
326
 
 
327
 
 
328
 
 
329
            <a href="#top">back to section top</a>
 
330
    </div>
 
331
 
 
332
 
 
333
 
 
334
    
 
335
    <A name="usage_lookup_checks"></a>
 
336
    
 
337
    <div class="subsection">
 
338
    
 
339
 
 
340
    <h3>Setting Filesystem Checks</h3>
 
341
    
 
342
    
 
343
 
 
344
<p>Another important flag on <code>TemplateLookup</code> is <code>filesystem_checks</code>.  This defaults to <code>True</code>, and says that each time a template is returned by the <code>get_template()</code> method, the revision time of the original template file is checked against the last time the template was loaded, and if the file is newer will reload its contents and recompile the template.  On a production system, seting <code>filesystem_checks</code> to <code>False</code> can afford a small to moderate performance increase (depending on the type of filesystem used).
 
345
</p>
 
346
 
 
347
 
 
348
 
 
349
    </div>
 
350
 
 
351
 
 
352
 
 
353
 
 
354
            <a href="#top">back to section top</a>
 
355
    </div>
 
356
 
 
357
 
 
358
 
 
359
    
 
360
    <A name="usage_using"></a>
 
361
    
 
362
    <div class="subsection">
 
363
    
 
364
 
 
365
    <h3>Using Unicode and Encoding</h3>
 
366
    
 
367
    
 
368
 
 
369
<p>Both <code>Template</code> and <code>TemplateLookup</code> accept <code>output_encoding</code> and <code>encoding_errors</code> parameters which can be used to encode the output in any Python supported codec:
 
370
</p>
 
371
 
 
372
    
 
373
    
 
374
 
 
375
    <div class="code">
 
376
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.template</span> <span class="k">import</span> <span class="n">Template</span>
 
377
<span class="k">from</span> <span class="nn">mako.lookup</span> <span class="k">import</span> <span class="n">TemplateLookup</span>
 
378
 
 
379
<span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/docs&#39;</span><span class="p">],</span> <span class="n">output_encoding</span><span class="o">=</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> <span class="n">encoding_errors</span><span class="o">=</span><span class="s">&#39;replace&#39;</span><span class="p">)</span>
 
380
 
 
381
<span class="n">mytemplate</span> <span class="o">=</span> <span class="n">mylookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&quot;foo.txt&quot;</span><span class="p">)</span>
 
382
<span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
383
</pre></div>
 
384
 
 
385
    </div>
 
386
<p>Additionally, the <code>render_unicode()</code> method exists which will return the template output as a Python <code>unicode</code> object:
 
387
</p>
 
388
 
 
389
    
 
390
    
 
391
 
 
392
    <div class="code">
 
393
        <div class="highlight" ><pre><span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">()</span>
 
394
</pre></div>
 
395
 
 
396
    </div>
 
397
<p>The above method disgards the output encoding keyword argument; you can encode yourself by saying:
 
398
</p>
 
399
 
 
400
    
 
401
    
 
402
 
 
403
    <div class="code">
 
404
        <div class="highlight" ><pre><span class="k">print</span> <span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">,</span> <span class="s">&#39;replace&#39;</span><span class="p">)</span>
 
405
</pre></div>
 
406
 
 
407
    </div>
 
408
<p>Note that Mako's ability to return data in any encoding and/or <code>unicode</code> implies that the underlying output stream of the template is a Python unicode object.  This behavior is described fully in <a href="unicode.html">The Unicode Chapter</a>.
 
409
</p>
 
410
 
 
411
 
 
412
 
 
413
            <a href="#top">back to section top</a>
 
414
    </div>
 
415
 
 
416
 
 
417
 
 
418
    
 
419
    <A name="usage_handling"></a>
 
420
    
 
421
    <div class="subsection">
 
422
    
 
423
 
 
424
    <h3>Handling Exceptions</h3>
 
425
    
 
426
    
 
427
 
 
428
<p>Template exceptions can occur in two distinct places.  One is when you <strong>lookup, parse and compile</strong> the template, the other is when you <strong>run</strong> the template.  Within the running of a template, exceptions are thrown normally from whatever Python code originated the issue.  Mako has its own set of exception classes which mostly apply to the lookup and lexer/compiler stages of template construction.  Mako provides some library routines that can be used to help provide Mako-specific information about any exception's stack trace, as well as formatting the exception within textual or HTML format.  In all cases, the main value of these handlers is that of converting Python filenames, line numbers, and code samples into Mako template filenames, line numbers, and code samples.  All lines within a stack trace which correspond to a Mako template module will be converted to be against the originating template file.
 
429
</p>
 
430
<p>To format exception traces, the <code>text_error_template</code> and <code>html_error_template</code> functions are provided.  They make usage of <code>sys.exc_info()</code> to get at the most recently thrown exception.  Usage of these handlers usually looks like:
 
431
</p>
 
432
 
 
433
    
 
434
    
 
435
 
 
436
    <div class="code">
 
437
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako</span> <span class="k">import</span> <span class="n">exceptions</span>
 
438
 
 
439
<span class="k">try</span><span class="p">:</span>
 
440
    <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
 
441
    <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
442
<span class="k">except</span><span class="p">:</span>
 
443
    <span class="k">print</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">text_error_template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
444
</pre></div>
 
445
 
 
446
    </div>
 
447
<p>Or for the HTML render function:
 
448
</p>
 
449
 
 
450
    
 
451
    
 
452
 
 
453
    <div class="code">
 
454
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako</span> <span class="k">import</span> <span class="n">exceptions</span>
 
455
 
 
456
<span class="k">try</span><span class="p">:</span>
 
457
    <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
 
458
    <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
459
<span class="k">except</span><span class="p">:</span>
 
460
    <span class="k">print</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">html_error_template</span><span class="p">()</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
461
</pre></div>
 
462
 
 
463
    </div>
 
464
<p>The <code>html_error_template</code> template accepts two options: specifying <code>full=False</code> causes only a section of an HTML document to be rendered. Specifying <code>css=False</code> will disable the default stylesheet from being rendered.
 
465
</p>
 
466
<p>E.g.:
 
467
</p>
 
468
 
 
469
    
 
470
    
 
471
 
 
472
    <div class="code">
 
473
        <div class="highlight" ><pre>    print exceptions.html_error_template().render(full=False)
 
474
</pre></div>
 
475
 
 
476
    </div>
 
477
<p>The HTML render function is also available built-in to <code>Template</code> using the <code>format_exceptions</code> flag.  In this case, any exceptions raised within the <strong>render</strong> stage of the template will result in the output being substituted with the output of <code>html_error_template</code>.
 
478
</p>
 
479
 
 
480
    
 
481
    
 
482
 
 
483
    <div class="code">
 
484
        <div class="highlight" ><pre><span class="n">template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s">&quot;/foo/bar&quot;</span><span class="p">,</span> <span class="n">format_exceptions</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
485
<span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
486
</pre></div>
 
487
 
 
488
    </div>
 
489
<p>Note that the compile stage of the above template occurs when you construct the <code>Template</code> itself, and no output stream is defined.  Therefore exceptions which occur within the lookup/parse/compile stage will not be handled and will propagate normally.   While the pre-render traceback usually will not include any Mako-specific lines anyway, it will mean that exceptions which occur previous to rendering and those which occur within rendering will be handled differently...so the <code>try/except</code> patterns described previously are probably of more general use.
 
490
</p>
 
491
<p>The underlying object used by the error template functions is the <code>RichTraceback</code> object.  This object can also be used directly to provide custom error views.  Here's an example usage which describes its general API:
 
492
</p>
 
493
 
 
494
    
 
495
    
 
496
 
 
497
    <div class="code">
 
498
        <div class="highlight" ><pre><span class="k">from</span> <span class="nn">mako.exceptions</span> <span class="k">import</span> <span class="n">RichTraceback</span>
 
499
 
 
500
<span class="k">try</span><span class="p">:</span>
 
501
    <span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
 
502
    <span class="k">print</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">()</span>
 
503
<span class="k">except</span><span class="p">:</span>
 
504
    <span class="n">traceback</span> <span class="o">=</span> <span class="n">RichTraceback</span><span class="p">()</span>
 
505
    <span class="k">for</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span> <span class="ow">in</span> <span class="n">traceback</span><span class="o">.</span><span class="n">traceback</span><span class="p">:</span>
 
506
        <span class="k">print</span> <span class="s">&quot;File </span><span class="si">%s</span><span class="s">, line </span><span class="si">%s</span><span class="s">, in </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">lineno</span><span class="p">,</span> <span class="n">function</span><span class="p">)</span>
 
507
        <span class="k">print</span> <span class="n">line</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
 
508
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">),</span> <span class="n">traceback</span><span class="o">.</span><span class="n">error</span><span class="p">)</span>
 
509
</pre></div>
 
510
 
 
511
    </div>
 
512
<p>Further information about <code>RichTraceback</code> is available within the module-level documentation for <code>mako.exceptions</code>.
 
513
</p>
 
514
 
 
515
 
 
516
 
 
517
            <a href="#top">back to section top</a>
 
518
    </div>
 
519
 
 
520
 
 
521
 
 
522
    
 
523
    <A name="usage_common"></a>
 
524
    
 
525
    <div class="subsection">
 
526
    
 
527
 
 
528
    <h3>Common Framework Integrations</h3>
 
529
    
 
530
    
 
531
 
 
532
<p>The Mako distribution includes a little bit of helper code for the purpose of using Mako in some popular web framework scenarios.  This is a brief description of whats included.
 
533
</p>
 
534
 
 
535
 
 
536
    
 
537
    <A name="usage_common_turbogears/pylons"></a>
 
538
    
 
539
    <div class="subsection">
 
540
    
 
541
 
 
542
    <h3>Turbogears/Pylons Plugin</h3>
 
543
    
 
544
    
 
545
 
 
546
<p>The standard plugin methodology used by <a href='http://www.turbogears.org'>Turbogears</a> as well as <a href='http://www.pylonshq.com'>Pylons</a> is included in the module <code>mako.ext.turbogears</code>, using the <code>TGPlugin</code> class.  This is also a setuptools entrypoint under the heading <code>python.templating.engines</code> with the name <code>mako</code>.
 
547
</p>
 
548
 
 
549
 
 
550
 
 
551
            <a href="#top">back to section top</a>
 
552
    </div>
 
553
 
 
554
 
 
555
 
 
556
    
 
557
    <A name="usage_common_wsgi"></a>
 
558
    
 
559
    <div class="subsection">
 
560
    
 
561
 
 
562
    <h3>WSGI</h3>
 
563
    
 
564
    
 
565
 
 
566
<p>A sample WSGI application is included in the distrubution in the file <code>examples/wsgi/run_wsgi.py</code>.  This runner is set up to pull files from a <code>templates</code> as well as an <code>htdocs</code> directory and includes a rudimental two-file layout.  The WSGI runner acts as a fully functional standalone web server, using <code>wsgiutils</code> to run itself, and propagates GET and POST arguments from the request into the <code>Context</code>, can serve images, css files and other kinds of files, and also displays errors using Mako's included exception-handling utilities.
 
567
</p>
 
568
 
 
569
 
 
570
 
 
571
            <a href="#top">back to section top</a>
 
572
    </div>
 
573
 
 
574
 
 
575
 
 
576
    
 
577
    <A name="usage_common_pygments"></a>
 
578
    
 
579
    <div class="subsection">
 
580
    
 
581
 
 
582
    <h3>Pygments</h3>
 
583
    
 
584
    
 
585
 
 
586
<p>A <a href='http://pygments.pocoo.org'>Pygments</a>-compatible syntax highlighting module is included under <code>mako.ext.pygmentplugin</code>.  This module is used in the generation of Mako documentation and also contains various setuptools entry points under the heading <code>pygments.lexers</code>, including <code>mako</code>, <code>html+mako</code>, <code>xml+mako</code> (see the <code>setup.py</code> file for all the entry points).
 
587
</p>
 
588
 
 
589
 
 
590
 
 
591
            <a href="#top">back to section top</a>
 
592
    </div>
 
593
 
 
594
 
 
595
 
 
596
    
 
597
    <A name="usage_common_babel"></a>
 
598
    
 
599
    <div class="subsection">
 
600
    
 
601
 
 
602
    <h3>Babel (Internationalization)</h3>
 
603
    
 
604
    
 
605
 
 
606
<p>Mako provides support for extracting gettext messages from templates via a <a href='http://babel.edgewall.org/'>Babel</a> extractor entry point under <code>mako.ext.babelplugin</code>.
 
607
</p>
 
608
<p>Gettext messages are extracted from all Python code sections, even the more obscure ones such as <a href="syntax.html#syntax_control">control structures</a>, <a href="defs.html">def tag function declarations</a>, <a href="defs.html#defs_defswithcontent">call tag exprs</a> and even <a href="syntax.html#syntax_tags_page">page tag args</a>.
 
609
</p>
 
610
<p><a href='http://babel.edgewall.org/wiki/Documentation/messages.html#comments-tags-and-translator-comments-explanation'>Translator comments</a> may also be extracted from Mako templates when a comment tag is specified to <a href='http://babel.edgewall.org/'>Babel</a> (such as with the -c option).
 
611
</p>
 
612
<p>For example, a project '<code>myproj</code>' contains the following Mako template at myproj/myproj/templates/name.html:
 
613
</p>
 
614
 
 
615
    
 
616
    
 
617
 
 
618
    <div class="code">
 
619
        <div class="highlight" ><pre><span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;name&quot;</span><span class="nt">&gt;</span>
 
620
  Name:
 
621
  ## TRANSLATORS: This is a proper name. See the gettext
 
622
  ## manual, section Names.
 
623
  <span class="cp">${</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Francois Pinard&#39;</span><span class="p">)</span><span class="cp">}</span>
 
624
<span class="nt">&lt;/div&gt;</span>
 
625
</pre></div>
 
626
 
 
627
    </div>
 
628
<p>To extract gettext messages from this template the project needs a Mako section in its <a href='http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration'>Babel Extraction Method Mapping file</a> (typically located at myproj/babel.cfg):
 
629
</p>
 
630
 
 
631
    
 
632
    
 
633
 
 
634
    <div class="code">
 
635
        <div class="highlight" ><pre><span class="c"># Extraction from Python source files</span>
 
636
 
 
637
<span class="k">[python: myproj/**.py]</span>
 
638
 
 
639
<span class="c"># Extraction from Mako templates</span>
 
640
 
 
641
<span class="k">[mako: myproj/templates/**.html]</span>
 
642
<span class="na">input_encoding</span> <span class="o">=</span> <span class="s">utf-8</span>
 
643
</pre></div>
 
644
 
 
645
    </div>
 
646
<p>The Mako extractor supports an optional <code>input_encoding</code> parameter specifying the encoding of the templates (identical to <code>Template</code>/<code>TemplateLookup</code>'s <code>input_encoding</code> parameter).
 
647
</p>
 
648
<p>Invoking <a href='http://babel.edgewall.org/'>Babel</a>'s extractor at the command line in the project's root directory:
 
649
</p>
 
650
 
 
651
    
 
652
    
 
653
 
 
654
    <div class="code">
 
655
        <div class="highlight" ><pre>myproj$ pybabel extract -F babel.cfg -c &quot;TRANSLATORS:&quot; .
 
656
</pre></div>
 
657
 
 
658
    </div>
 
659
<p>Will output a gettext catalog to stdout including the following:
 
660
</p>
 
661
 
 
662
    
 
663
    
 
664
 
 
665
    <div class="code">
 
666
        <div class="highlight" ><pre>#. This is a proper name. See the gettext
 
667
#. manual, section Names.
 
668
#: myproj/templates/name.html:5
 
669
msgid &quot;Francois Pinard&quot;
 
670
msgstr &quot;&quot;
 
671
</pre></div>
 
672
 
 
673
    </div>
 
674
<p>This is only a basic example: <a href='http://babel.edgewall.org/'>Babel</a> can be invoked from setup.py and its command line options specified in the accompanying setup.cfg via <a href='http://babel.edgewall.org/wiki/Documentation/setup.html'>Babel Distutils/Setuptools Integration</a>.
 
675
</p>
 
676
<p>Comments must immediately precede a gettext message to be extracted. In the following case the TRANSLATORS: comment would not have been extracted:
 
677
</p>
 
678
 
 
679
    
 
680
    
 
681
 
 
682
    <div class="code">
 
683
        <div class="highlight" ><pre><span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;name&quot;</span><span class="nt">&gt;</span>
 
684
  ## TRANSLATORS: This is a proper name. See the gettext
 
685
  ## manual, section Names.
 
686
  Name: <span class="cp">${</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Francois Pinard&#39;</span><span class="p">)</span><span class="cp">}</span>
 
687
<span class="nt">&lt;/div&gt;</span>
 
688
</pre></div>
 
689
 
 
690
    </div>
 
691
<p>See the <a href='http://babel.edgewall.org/wiki/Documentation/index.html'>Babel User Guide</a> for more information.
 
692
</p>
 
693
 
 
694
 
 
695
 
 
696
 
 
697
    </div>
 
698
 
 
699
 
 
700
 
 
701
 
 
702
    </div>
 
703
 
 
704
 
 
705
 
 
706
 
 
707
            <a href="#top">back to section top</a>
 
708
    </div>
 
709
 
 
710
 
 
711
</html>
 
712
 
 
713
 
 
714
    <div class="toolbar">
 
715
        <div class="prevnext">
 
716
 
 
717
            
 
718
            Next: <a href="syntax.html">Syntax</a>
 
719
        </div>
 
720
        <h3><a href="index.html">Table of Contents</a></h3>
 
721
    </div>
 
722
 
 
723
 
 
724
 
 
725
 
 
726
 
 
727
 
 
728
</body>
 
729
</html>
 
730
 
 
731
 
 
732
 
 
733
 
 
734