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

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/doc/syntax.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 - Syntax</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
            Previous: <a href="usage.html">Basic Usage</a>
 
48
 
 
49
            |
 
50
            Next: <a href="defs.html">Defs</a>
 
51
        </div>
 
52
        <h3><a href="index.html">Table of Contents</a></h3>
 
53
    </div>
 
54
 
 
55
 
 
56
    <br/>
 
57
        <a href="#syntax">Syntax</a>
 
58
        
 
59
        
 
60
    <ul>
 
61
        <li><A style="" href="syntax.html#syntax_expression">Expression Substiution</a></li>
 
62
        
 
63
            
 
64
    <ul>
 
65
        <li><A style="" href="syntax.html#syntax_expression_expression">Expression Escaping</a></li>
 
66
        
 
67
            
 
68
    <ul>
 
69
    </ul>
 
70
 
 
71
    </ul>
 
72
 
 
73
        <li><A style="" href="syntax.html#syntax_control">Control Structures</a></li>
 
74
        
 
75
            
 
76
    <ul>
 
77
    </ul>
 
78
 
 
79
        <li><A style="" href="syntax.html#syntax_comments">Comments</a></li>
 
80
        
 
81
            
 
82
    <ul>
 
83
    </ul>
 
84
 
 
85
        <li><A style="" href="syntax.html#syntax_newline">Newline Filters</a></li>
 
86
        
 
87
            
 
88
    <ul>
 
89
    </ul>
 
90
 
 
91
        <li><A style="" href="syntax.html#syntax_python">Python Blocks</a></li>
 
92
        
 
93
            
 
94
    <ul>
 
95
        <li><A style="" href="syntax.html#syntax_python_module-level">Module-level Blocks</a></li>
 
96
        
 
97
            
 
98
    <ul>
 
99
    </ul>
 
100
 
 
101
    </ul>
 
102
 
 
103
        <li><A style="" href="syntax.html#syntax_tags">Tags</a></li>
 
104
        
 
105
            
 
106
    <ul>
 
107
        <li><A style="" href="syntax.html#syntax_tags_page"><%page></a></li>
 
108
        
 
109
            
 
110
    <ul>
 
111
    </ul>
 
112
 
 
113
        <li><A style="" href="syntax.html#syntax_tags_include"><%include></a></li>
 
114
        
 
115
            
 
116
    <ul>
 
117
    </ul>
 
118
 
 
119
        <li><A style="" href="syntax.html#syntax_tags_def"><%def></a></li>
 
120
        
 
121
            
 
122
    <ul>
 
123
    </ul>
 
124
 
 
125
        <li><A style="" href="syntax.html#syntax_tags_namespace"><%namespace></a></li>
 
126
        
 
127
            
 
128
    <ul>
 
129
    </ul>
 
130
 
 
131
        <li><A style="" href="syntax.html#syntax_tags_inherit"><%inherit></a></li>
 
132
        
 
133
            
 
134
    <ul>
 
135
    </ul>
 
136
 
 
137
        <li><A style="" href="syntax.html#syntax_tags_call"><%call></a></li>
 
138
        
 
139
            
 
140
    <ul>
 
141
    </ul>
 
142
 
 
143
        <li><A style="" href="syntax.html#syntax_tags_doc"><%doc></a></li>
 
144
        
 
145
            
 
146
    <ul>
 
147
    </ul>
 
148
 
 
149
        <li><A style="" href="syntax.html#syntax_tags_text"><%text></a></li>
 
150
        
 
151
            
 
152
    <ul>
 
153
    </ul>
 
154
 
 
155
    </ul>
 
156
 
 
157
    </ul>
 
158
 
 
159
        </div>
 
160
 
 
161
 
 
162
 
 
163
 
 
164
 
 
165
 
 
166
 
 
167
 
 
168
 
 
169
 
 
170
 
 
171
    
 
172
    <A name="syntax"></a>
 
173
    
 
174
    <div class="section">
 
175
    
 
176
 
 
177
    <h3>Syntax</h3>
 
178
    
 
179
    
 
180
 
 
181
<p>A Mako template is parsed from a text stream containing any kind of content, XML, HTML, email text, etc.  The template can further contain Mako-specific directives which represent variable and/or expression substitutions, control structures (i.e. conditionals and loops), server-side comments, full blocks of Python code, as well as various tags that offer additional functionality.  All of these constructs compile into real Python code.  This means that you can leverage the full power of Python in almost every aspect of a Mako template.
 
182
</p>
 
183
 
 
184
 
 
185
    
 
186
    <A name="syntax_expression"></a>
 
187
    
 
188
    <div class="subsection">
 
189
    
 
190
 
 
191
    <h3>Expression Substiution</h3>
 
192
    
 
193
    
 
194
 
 
195
<p>The simplest expression is just a variable substitution.  The syntax for this is the <code>${}</code> construct, which is inspired by Perl, Genshi, JSP EL, and others:
 
196
</p>
 
197
 
 
198
    
 
199
    
 
200
 
 
201
    <div class="code">
 
202
        <div class="highlight" ><pre>this is x: <span class="cp">${</span><span class="n">x</span><span class="cp">}</span>
 
203
</pre></div>
 
204
 
 
205
    </div>
 
206
<p>Above, the string representation of <code>x</code> is applied to the template's output stream.  If you're wondering where <code>x</code> comes from, its usually from the <code>Context</code> supplied to the template's rendering function.  If <code>x</code> was not supplied to the template and was not otherwise assigned locally, it evaluates to a special value <code>UNDEFINED</code>.  More on that later.
 
207
</p>
 
208
<p>The contents within the <code>${}</code> tag are evaluated by Python directly, so full expressions are OK:
 
209
</p>
 
210
 
 
211
    
 
212
    
 
213
 
 
214
    <div class="code">
 
215
        <div class="highlight" ><pre>pythagorean theorem:  <span class="cp">${</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="nb">pow</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="cp">}</span>
 
216
</pre></div>
 
217
 
 
218
    </div>
 
219
<p>The results of the expression are evaluated into a string result in all cases before being rendered to the output stream, such as the above example where the expression produces a numeric result.
 
220
</p>
 
221
 
 
222
 
 
223
    
 
224
    <A name="syntax_expression_expression"></a>
 
225
    
 
226
    <div class="subsection">
 
227
    
 
228
 
 
229
    <h3>Expression Escaping</h3>
 
230
    
 
231
    
 
232
 
 
233
<p>Mako includes a number of built-in escaping mechanisms, including HTML, URI and XML escaping, as well as a "trim" function.  These escapes can be added to an expression substituion using the <code>|</code> operator:
 
234
</p>
 
235
 
 
236
    
 
237
    
 
238
 
 
239
    <div class="code">
 
240
        <div class="highlight" ><pre><span class="cp">${</span><span class="s">&quot;this is some text&quot;</span> <span class="o">|</span> <span class="n">u</span><span class="cp">}</span>
 
241
</pre></div>
 
242
 
 
243
    </div>
 
244
<p>The above expression applies URL escaping to the expression, and produces <code>this+is+some+text</code>.  The <code>u</code> name indicates URL escaping, whereas <code>h</code> represents HTML escaping, <code>x</code> represents XML escaping, and <code>trim</code> applies a trim function.
 
245
</p>
 
246
<p>Read more about built in filtering functions, including how to make your own filter functions, in <a href="filtering.html">Filtering and Buffering</a>.
 
247
</p>
 
248
 
 
249
 
 
250
 
 
251
    </div>
 
252
 
 
253
 
 
254
 
 
255
 
 
256
            <a href="#top">back to section top</a>
 
257
    </div>
 
258
 
 
259
 
 
260
 
 
261
    
 
262
    <A name="syntax_control"></a>
 
263
    
 
264
    <div class="subsection">
 
265
    
 
266
 
 
267
    <h3>Control Structures</h3>
 
268
    
 
269
    
 
270
 
 
271
<p>A control structure refers to all those things that control the flow of a program - conditionals (i.e. if/else), loops (like while and for), as well as things like <code>try/except</code>.  In Mako, control structures are written using the <code>%</code> marker followed by a regular Python control expression, and are "closed" by using another <code>%</code> marker with the tag "<code>end&lt;name&gt;</code>", where "<code>&lt;name&gt;</code>" is the keyword of the expression:
 
272
</p>
 
273
 
 
274
    
 
275
    
 
276
 
 
277
    <div class="code">
 
278
        <div class="highlight" ><pre><span class="cp">%</span> <span class="k">if</span> <span class="n">x</span><span class="o">==</span><span class="mi">5</span><span class="p">:</span>
 
279
    this is some output
 
280
<span class="cp">%</span><span class="k"> endif</span>
 
281
</pre></div>
 
282
 
 
283
    </div>
 
284
<p>The <code>%</code> can appear anywhere on the line as long as no text precedes it; indentation is not signficant.  The full range of Python "colon" expressions are allowed here, including <code>if/elif/else</code>, <code>while</code>, <code>for</code>, and even <code>def</code>, although Mako has a built-in tag for defs which is more full-featured.
 
285
</p>
 
286
 
 
287
    
 
288
    
 
289
 
 
290
    <div class="code">
 
291
        <div class="highlight" ><pre><span class="cp">%</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;one&#39;</span><span class="p">,</span> <span class="s">&#39;two&#39;</span><span class="p">,</span> <span class="s">&#39;three&#39;</span><span class="p">,</span> <span class="s">&#39;four&#39;</span><span class="p">,</span> <span class="s">&#39;five&#39;</span><span class="p">]:</span>
 
292
    <span class="cp">%</span> <span class="k">if</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;t&#39;</span><span class="p">:</span>
 
293
     its two or three
 
294
    <span class="cp">%</span> <span class="k">elif</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;f&#39;</span><span class="p">:</span>
 
295
    four/five
 
296
    <span class="cp">%</span> <span class="k">else</span><span class="p">:</span>
 
297
    one
 
298
    <span class="cp">%</span><span class="k">endif</span>
 
299
<span class="cp">%</span><span class="k"> endfor</span>
 
300
</pre></div>
 
301
 
 
302
    </div>
 
303
 
 
304
 
 
305
 
 
306
            <a href="#top">back to section top</a>
 
307
    </div>
 
308
 
 
309
 
 
310
 
 
311
    
 
312
    <A name="syntax_comments"></a>
 
313
    
 
314
    <div class="subsection">
 
315
    
 
316
 
 
317
    <h3>Comments</h3>
 
318
    
 
319
    
 
320
 
 
321
<p>Comments come in two varieties.  The single line comment uses <code>##</code> as the first non-space characters on a line:
 
322
</p>
 
323
 
 
324
    
 
325
    
 
326
 
 
327
    <div class="code">
 
328
        <div class="highlight" ><pre><span class="cp">## this is a comment.</span>
 
329
...text ...
 
330
</pre></div>
 
331
 
 
332
    </div>
 
333
<p>A multiline version exists using <code>&lt;%doc&gt;  ...text... &lt;/%doc&gt;</code>:
 
334
</p>
 
335
 
 
336
    
 
337
    
 
338
 
 
339
    <div class="code">
 
340
        <div class="highlight" ><pre><span class="cp">&lt;%doc&gt;</span>
 
341
<span class="cp">    these are comments</span>
 
342
<span class="cp">    more comments</span>
 
343
<span class="cp">&lt;/%doc&gt;</span>
 
344
</pre></div>
 
345
 
 
346
    </div>
 
347
<p>Note that this is <strong>new behavior as of Mako 0.1.3</strong>.  The syntax prior to this version was the single pound sign (<code>#</code>), which was agreed by the Mako userbase that it conflicted with CSS elements too often and also did not address multiline comments easily.
 
348
</p>
 
349
 
 
350
 
 
351
 
 
352
            <a href="#top">back to section top</a>
 
353
    </div>
 
354
 
 
355
 
 
356
 
 
357
    
 
358
    <A name="syntax_newline"></a>
 
359
    
 
360
    <div class="subsection">
 
361
    
 
362
 
 
363
    <h3>Newline Filters</h3>
 
364
    
 
365
    
 
366
 
 
367
<p>The backslash ("<code>\</code>") character, placed at the end of any line, will consume the newline character before continuing to the next line:
 
368
</p>
 
369
 
 
370
    
 
371
    
 
372
 
 
373
    <div class="code">
 
374
        <div class="highlight" ><pre>here is a line that goes onto <span class="o">\</span>
 
375
<span class="o"></span>another line.
 
376
</pre></div>
 
377
 
 
378
    </div>
 
379
<p>The above text evaluates to:
 
380
</p>
 
381
 
 
382
    
 
383
    
 
384
 
 
385
    <div class="code">
 
386
        <div class="highlight" ><pre>here is a line that goes onto another line.
 
387
</pre></div>
 
388
 
 
389
    </div>
 
390
 
 
391
 
 
392
 
 
393
            <a href="#top">back to section top</a>
 
394
    </div>
 
395
 
 
396
 
 
397
 
 
398
    
 
399
    <A name="syntax_python"></a>
 
400
    
 
401
    <div class="subsection">
 
402
    
 
403
 
 
404
    <h3>Python Blocks</h3>
 
405
    
 
406
    
 
407
 
 
408
<p>Any arbitrary block of python can be dropped in using the <code>&lt;% %&gt;</code> tags:
 
409
</p>
 
410
 
 
411
    
 
412
    
 
413
 
 
414
    <div class="code">
 
415
        <div class="highlight" ><pre>this is a template
 
416
<span class="cp">&lt;%</span>
 
417
    <span class="n">x</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">get_resource</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">)</span>
 
418
    <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">z</span><span class="o">.</span><span class="n">element</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="n">x</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">frobnizzle</span><span class="o">==</span><span class="mi">5</span><span class="p">]</span>
 
419
<span class="cp">%&gt;</span>
 
420
<span class="cp">%</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">y</span><span class="p">:</span>
 
421
    element: <span class="cp">${</span><span class="n">y</span><span class="cp">}</span>
 
422
<span class="cp">%</span><span class="k"> endfor</span>
 
423
</pre></div>
 
424
 
 
425
    </div>
 
426
<p>Within <code>&lt;% %&gt;</code>, you're writing a regular block of Python code.  While the code can appear with an arbitrary level of preceding whitespace,  it has to be consistently formatted with itself.  Mako's compiler will adjust the block of Python to be consistent with the surrounding generated Python code.
 
427
</p>
 
428
 
 
429
 
 
430
    
 
431
    <A name="syntax_python_module-level"></a>
 
432
    
 
433
    <div class="subsection">
 
434
    
 
435
 
 
436
    <h3>Module-level Blocks</h3>
 
437
    
 
438
    
 
439
 
 
440
<p>A variant on <code>&lt;% %&gt;</code> is the module-level code block, denoted by <code>&lt;%! %&gt;</code>.  Code within these tags is executed at the module level of the template, and not within the rendering function of the template.  Therefore, this code does not have access to the template's context and is only executed when the template is loaded into memory (which can be only once per application, or more, depending on the runtime environment).  Use the <code>&lt;%! %&gt;</code> tags to declare your template's imports, as well as any pure-Python functions you might want to declare:
 
441
</p>
 
442
 
 
443
    
 
444
    
 
445
 
 
446
    <div class="code">
 
447
        <div class="highlight" ><pre><span class="cp">&lt;%!</span>
 
448
    <span class="k">import</span> <span class="nn">mylib</span>
 
449
    <span class="k">import</span> <span class="nn">re</span>
 
450
 
 
451
    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
 
452
        <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;^@&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
 
453
<span class="cp">%&gt;</span>
 
454
</pre></div>
 
455
 
 
456
    </div>
 
457
<p>Any number of <code>&lt;%! %&gt;</code> blocks can be declared anywhere in a template; they will be rendered in the resulting module in the order that they appear.
 
458
</p>
 
459
 
 
460
 
 
461
 
 
462
    </div>
 
463
 
 
464
 
 
465
 
 
466
 
 
467
            <a href="#top">back to section top</a>
 
468
    </div>
 
469
 
 
470
 
 
471
 
 
472
    
 
473
    <A name="syntax_tags"></a>
 
474
    
 
475
    <div class="subsection">
 
476
    
 
477
 
 
478
    <h3>Tags</h3>
 
479
    
 
480
    
 
481
 
 
482
<p>The rest of what Mako offers takes place in the form of tags.  All tags use the same syntax, which is similar to an XML tag except that the first character of the tag name is a <code>%</code> character.  The tag is closed either by a contained slash character, or an explicit closing tag:
 
483
</p>
 
484
 
 
485
    
 
486
    
 
487
 
 
488
    <div class="code">
 
489
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;foo.txt&quot;</span><span class="cp">/&gt;</span>
 
490
 
 
491
<span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;foo&quot;</span> <span class="na">buffered=</span><span class="s">&quot;True&quot;</span><span class="cp">&gt;</span>
 
492
    this is a def
 
493
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
 
494
</pre></div>
 
495
 
 
496
    </div>
 
497
<p>All tags have a set of attributes which are defined for each tag.  Some of these attributes are required.  Also, many attributes support <strong>evaluation</strong>, meaning you can embed an expression (using <code>${}</code>) inside the attribute text:
 
498
</p>
 
499
 
 
500
    
 
501
    
 
502
 
 
503
    <div class="code">
 
504
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;/foo/bar/${myfile}.txt&quot;</span><span class="cp">/&gt;</span>
 
505
</pre></div>
 
506
 
 
507
    </div>
 
508
<p>Whether or not an attribute accepts runtime evaluation depends on the type of tag and how that tag is compiled into the template.  The best way to find out if you can stick an expression in is to try it !  The lexer will tell you if its not valid.
 
509
</p>
 
510
<p>Heres a quick summary of all the tags:
 
511
</p>
 
512
 
 
513
 
 
514
    
 
515
    <A name="syntax_tags_page"></a>
 
516
    
 
517
    <div class="subsection">
 
518
    
 
519
 
 
520
    <h3><%page></h3>
 
521
    
 
522
    
 
523
 
 
524
<p>This tag defines general characteristics of the template, including caching arguments, and optional lists of arguments which the template expects when invoked.
 
525
</p>
 
526
 
 
527
    
 
528
    
 
529
 
 
530
    <div class="code">
 
531
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">args=</span><span class="s">&quot;x, y, z=&#39;default&#39;&quot;</span><span class="cp">/&gt;</span>
 
532
</pre></div>
 
533
 
 
534
    </div>
 
535
<p>Or a page tag that defines caching characteristics:
 
536
</p>
 
537
 
 
538
    
 
539
    
 
540
 
 
541
    <div class="code">
 
542
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_type=</span><span class="s">&quot;memory&quot;</span><span class="cp">/&gt;</span>
 
543
</pre></div>
 
544
 
 
545
    </div>
 
546
<p>Currently, only one <code>&lt;%page&gt;</code> tag gets used per template, the rest get ignored.  While this will be improved in a future release, for now make sure you have only one <code>&lt;%page&gt;</code> tag defined in your template, else you may not get the results you want.  The details of what <code>&lt;%page&gt;</code> is used for are described further in <a href="namespaces.html#namespaces_body">The "body()" method</a> as well as <a href="caching.html">Caching</a>.
 
547
</p>
 
548
 
 
549
 
 
550
 
 
551
            <a href="#top">back to section top</a>
 
552
    </div>
 
553
 
 
554
 
 
555
 
 
556
    
 
557
    <A name="syntax_tags_include"></a>
 
558
    
 
559
    <div class="subsection">
 
560
    
 
561
 
 
562
    <h3><%include></h3>
 
563
    
 
564
    
 
565
 
 
566
<p>A tag that is familiar from other template languages, %include is a regular joe that just accepts a file argument and calls in the rendered result of that file:
 
567
</p>
 
568
 
 
569
    
 
570
    
 
571
 
 
572
    <div class="code">
 
573
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;header.html&quot;</span><span class="cp">/&gt;</span>
 
574
 
 
575
    hello world
 
576
 
 
577
<span class="cp">&lt;%</span><span class="nb">include</span> <span class="na">file=</span><span class="s">&quot;footer.html&quot;</span><span class="cp">/&gt;</span>
 
578
</pre></div>
 
579
 
 
580
    </div>
 
581
 
 
582
 
 
583
 
 
584
            <a href="#top">back to section top</a>
 
585
    </div>
 
586
 
 
587
 
 
588
 
 
589
    
 
590
    <A name="syntax_tags_def"></a>
 
591
    
 
592
    <div class="subsection">
 
593
    
 
594
 
 
595
    <h3><%def></h3>
 
596
    
 
597
    
 
598
 
 
599
<p>The <code>%def</code> tag defines a Python function which contains a set of content, that can be called at some other point in the template.  The basic idea is simple:
 
600
</p>
 
601
 
 
602
    
 
603
    
 
604
 
 
605
    <div class="code">
 
606
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;myfunc(x)&quot;</span><span class="cp">&gt;</span>
 
607
    this is myfunc, x is <span class="cp">${</span><span class="n">x</span><span class="cp">}</span>
 
608
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
 
609
 
 
610
<span class="cp">${</span><span class="n">myfunc</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="cp">}</span>
 
611
</pre></div>
 
612
 
 
613
    </div>
 
614
<p>The %def tag is a lot more powerful than a plain Python def, as the Mako compiler provides many extra services with %def that you wouldn't normally have, such as the ability to export defs as template "methods", automatic propigation of the current <code>Context</code>, buffering/filtering/caching flags, and def calls with content, which enable packages of defs to be sent as arguments to other def calls (not as hard as it sounds).  Get the full deal on what %def can do in <a href="defs.html">Defs</a>.
 
615
</p>
 
616
 
 
617
 
 
618
 
 
619
            <a href="#top">back to section top</a>
 
620
    </div>
 
621
 
 
622
 
 
623
 
 
624
    
 
625
    <A name="syntax_tags_namespace"></a>
 
626
    
 
627
    <div class="subsection">
 
628
    
 
629
 
 
630
    <h3><%namespace></h3>
 
631
    
 
632
    
 
633
 
 
634
<p>%namespace is Mako's equivalent of Python's <code>import</code> statement.  It allows access to all the rendering functions and metadata of other template files, plain Python modules, as well as locally defined "packages" of functions.
 
635
</p>
 
636
 
 
637
    
 
638
    
 
639
 
 
640
    <div class="code">
 
641
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">namespace</span> <span class="na">file=</span><span class="s">&quot;functions.html&quot;</span> <span class="na">import=</span><span class="s">&quot;*&quot;</span><span class="cp">/&gt;</span>
 
642
</pre></div>
 
643
 
 
644
    </div>
 
645
<p>The underlying object generated by %namespace, an instance of <code>mako.runtime.Namespace</code>, is a central construct used in templates to reference template-specific information such as the current URI, inheritance structures, and other things that are not as hard as they sound right here.  Namespaces are described in <a href="namespaces.html">Namespaces</a>.
 
646
</p>
 
647
 
 
648
 
 
649
 
 
650
            <a href="#top">back to section top</a>
 
651
    </div>
 
652
 
 
653
 
 
654
 
 
655
    
 
656
    <A name="syntax_tags_inherit"></a>
 
657
    
 
658
    <div class="subsection">
 
659
    
 
660
 
 
661
    <h3><%inherit></h3>
 
662
    
 
663
    
 
664
 
 
665
<p>Inherit allows templates to arrange themselves in <strong>inheritance chains</strong>.  This is a concept familiar in many other template languages.
 
666
</p>
 
667
 
 
668
    
 
669
    
 
670
 
 
671
    <div class="code">
 
672
        <div class="highlight" ><pre><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>
 
673
</pre></div>
 
674
 
 
675
    </div>
 
676
<p>When using the %inherit tag, control is passed to the topmost inherited template first, which then decides how to handle calling areas of content from its inheriting templates.  Mako offers a lot of flexbility in this area, including dynamic inheritance, content wrapping, and polymorphic method calls.  Check it out in <a href="inheritance.html">Inheritance</a>.
 
677
</p>
 
678
 
 
679
 
 
680
 
 
681
            <a href="#top">back to section top</a>
 
682
    </div>
 
683
 
 
684
 
 
685
 
 
686
    
 
687
    <A name="syntax_tags_call"></a>
 
688
    
 
689
    <div class="subsection">
 
690
    
 
691
 
 
692
    <h3><%call></h3>
 
693
    
 
694
    
 
695
 
 
696
<p>The call tag is used to call <code>&lt;%defs&gt;</code> with additional embedded content.  This tag is described in <a href="defs.html#defs_defswithcontent">Calling a def with embedded content and/or other defs</a>.
 
697
</p>
 
698
 
 
699
 
 
700
 
 
701
            <a href="#top">back to section top</a>
 
702
    </div>
 
703
 
 
704
 
 
705
 
 
706
    
 
707
    <A name="syntax_tags_doc"></a>
 
708
    
 
709
    <div class="subsection">
 
710
    
 
711
 
 
712
    <h3><%doc></h3>
 
713
    
 
714
    
 
715
 
 
716
<p>The doc tag handles multiline comments:
 
717
</p>
 
718
 
 
719
    
 
720
    
 
721
 
 
722
    <div class="code">
 
723
        <div class="highlight" ><pre><span class="cp">&lt;%doc&gt;</span>
 
724
<span class="cp">    these are comments</span>
 
725
<span class="cp">    more comments</span>
 
726
<span class="cp">&lt;/%doc&gt;</span>
 
727
</pre></div>
 
728
 
 
729
    </div>
 
730
<p>Also the <code>##</code> symbol as the first non-space characters on a line can be used for single line comments.
 
731
</p>
 
732
 
 
733
 
 
734
 
 
735
            <a href="#top">back to section top</a>
 
736
    </div>
 
737
 
 
738
 
 
739
 
 
740
    
 
741
    <A name="syntax_tags_text"></a>
 
742
    
 
743
    <div class="subsection">
 
744
    
 
745
 
 
746
    <h3><%text></h3>
 
747
    
 
748
    
 
749
 
 
750
<p>This tag suspends the Mako lexer's normal parsing of Mako template directives, and returns its entire body contents as plain text.  It is used pretty much to write documentation about Mako:
 
751
</p>
 
752
 
 
753
    
 
754
    
 
755
 
 
756
    <div class="code">
 
757
        <div class="highlight" ><pre><span class="cp">&lt;%</span><span class="nb">text</span> <span class="na">filter=</span><span class="s">&quot;h&quot;</span><span class="cp">&gt;</span>
 
758
    heres some fake mako <span class="cp">${</span><span class="n">syntax</span><span class="cp">}</span>
 
759
    <span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;x()&quot;</span><span class="cp">&gt;${</span><span class="n">x</span><span class="cp">}&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
 
760
<span class="cp">&lt;/%</span><span class="nb">text</span><span class="cp">&gt;</span>
 
761
</pre></div>
 
762
 
 
763
    </div>
 
764
 
 
765
 
 
766
 
 
767
 
 
768
    </div>
 
769
 
 
770
 
 
771
 
 
772
 
 
773
    </div>
 
774
 
 
775
 
 
776
 
 
777
 
 
778
            <a href="#top">back to section top</a>
 
779
    </div>
 
780
 
 
781
 
 
782
</html>
 
783
 
 
784
 
 
785
    <div class="toolbar">
 
786
        <div class="prevnext">
 
787
            Previous: <a href="usage.html">Basic Usage</a>
 
788
 
 
789
            |
 
790
            Next: <a href="defs.html">Defs</a>
 
791
        </div>
 
792
        <h3><a href="index.html">Table of Contents</a></h3>
 
793
    </div>
 
794
 
 
795
 
 
796
 
 
797
 
 
798
 
 
799
 
 
800
</body>
 
801
</html>
 
802
 
 
803
 
 
804
 
 
805
 
 
806