3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
4
<title>Mako Documentation - Filtering and Buffering</title>
6
<link rel="stylesheet" href="docs.css"></link>
7
<link rel="stylesheet" href="highlight.css"></link>
25
<div id="topanchor"><a name="top"> </a></div>
27
<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>
29
<h1>Mako Documentation</h1>
31
<div class="versionheader">Version: 0.1.9 Last Updated: 09/25/07 16:19:18</div>
46
<div class="prevnext">
47
Previous: <a href="inheritance.html">Inheritance</a>
50
Next: <a href="unicode.html">The Unicode Chapter</a>
52
<h3><a href="index.html">Table of Contents</a></h3>
57
<a href="#filtering">Filtering and Buffering</a>
61
<li><A style="" href="filtering.html#filtering_expression">Expression Filtering</a></li>
65
<li><A style="" href="filtering.html#filtering_expression_defaultfilters">The default_filters Argument</a></li>
71
<li><A style="" href="filtering.html#filtering_expression_turning">Turning off Filtering with the "n" filter</a></li>
79
<li><A style="" href="filtering.html#filtering_filtering">Filtering Defs</a></li>
85
<li><A style="" href="filtering.html#filtering_buffering">Buffering</a></li>
106
<A name="filtering"></a>
108
<div class="section">
111
<h3>Filtering and Buffering</h3>
118
<A name="filtering_expression"></a>
120
<div class="subsection">
123
<h3>Expression Filtering</h3>
127
<p>As described in the Syntax chapter, the "<code>|</code>" operator can be applied to a "<code>${}</code>" expression to apply escape filters to the output:
134
<div class="highlight" ><pre><span class="cp">${</span><span class="s">"this is some text"</span> <span class="o">|</span> <span class="n">u</span><span class="cp">}</span>
138
<p>The above expression applies URL escaping to the expression, and produces <code>this+is+some+text</code>.
140
<p>The built-in escape flags are:
144
<code>u</code> : URL escaping, provided by <code>urllib.quote_plus(string.encode('utf-8'))</code>
148
<code>h</code> : HTML escaping, provided by <code>cgi.escape(string, True)</code>
152
<code>x</code> : XML escaping
156
<code>trim</code> : whitespace trimming, provided by <code>string.strip()</code>
160
<code>entity</code> : produces HTML entity references for applicable strings, derived from <code>htmlentitydefs</code>
164
<code>unicode</code> : produces a Python unicode string (this function is applied by default).
168
<code>decode.<some encoding></code> : decode input into a Python unicode with the specified encoding
172
<code>n</code> : disable all default filtering; only filters specified in the local expression tag will be applied.
175
<p>To apply more than one filter, separate them by a comma:
182
<div class="highlight" ><pre><span class="cp">${</span><span class="s">" <tag>some value</tag> "</span> <span class="o">|</span> <span class="n">h</span><span class="p">,</span><span class="n">trim</span><span class="cp">}</span>
186
<p>The above produces <code>&lt;tag&gt;some value&lt;/tag&gt;</code>, with no leading or trailing whitespace. The HTML escaping function is applied first, the "trim" function second.
188
<p>Naturally, you can make your own filters too. A filter is just a Python function that accepts a single string argument, and returns the filtered result. The expressions after the <code>|</code> operator draw upon the local namespace of the template in which they appear, meaning you can define escaping functions locally:
195
<div class="highlight" ><pre><span class="cp"><%!</span>
196
<span class="k">def</span> <span class="nf">myescape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
197
<span class="k">return</span> <span class="s">"<TAG>"</span> <span class="o">+</span> <span class="n">text</span> <span class="o">+</span> <span class="s">"</TAG>"</span>
198
<span class="cp">%></span>
200
Heres some tagged text: <span class="cp">${</span><span class="s">"text"</span> <span class="o">|</span> <span class="n">myescape</span><span class="cp">}</span>
204
<p>Or from any Python module:
211
<div class="highlight" ><pre><span class="cp"><%!</span>
212
<span class="k">import</span> <span class="nn">myfilters</span>
213
<span class="cp">%></span>
215
Heres some tagged text: <span class="cp">${</span><span class="s">"text"</span> <span class="o">|</span> <span class="n">myfilters</span><span class="o">.</span><span class="n">tagfilter</span><span class="cp">}</span>
219
<p>A page can apply a default set of filters to all expression tags using the <code>expression_filter</code> argument to the <code>%page</code> tag:
226
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">page</span> <span class="na">expression_filter=</span><span class="s">"h"</span><span class="cp">/></span>
228
Escaped text: <span class="cp">${</span><span class="s">"<html>some html</html>"</span><span class="cp">}</span>
239
<div class="highlight" ><pre>Escaped text: <span class="ni">&lt;</span>html<span class="ni">&gt;</span>some html<span class="ni">&lt;</span>/html<span class="ni">&gt;</span>
246
<A name="filtering_expression_defaultfilters"></a>
248
<div class="subsection">
251
<h3>The default_filters Argument</h3>
255
<p><strong>New in version 0.1.2</strong>
257
<p>In addition to the <code>expression_filter</code> argument, the <code>default_filters</code> argument to both <code>Template</code> and <code>TemplateLookup</code> can specify filtering for all expression tags at the programmatic level. This array-based argument defaults to <code>["unicode"]</code>:
264
<div class="highlight" ><pre><span class="n">t</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">'/tmp'</span><span class="p">],</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">'unicode'</span><span class="p">])</span>
268
<p>To replace the usual <code>unicode</code> function with a specific encoding, the <code>decode</code> filter can be substituted:
275
<div class="highlight" ><pre><span class="n">t</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">'/tmp'</span><span class="p">],</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">'decode.utf8'</span><span class="p">])</span>
279
<p>Any string name can be added to <code>default_filters</code> where it will be added to all expressions as a filter. The filters are applied from left to right, meaning the leftmost filter is applied first.
286
<div class="highlight" ><pre><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">templatetext</span><span class="p">,</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">'unicode'</span><span class="p">,</span> <span class="s">'myfilter'</span><span class="p">])</span>
290
<p>To ease the usage of <code>default_filters</code> with custom filters, you can also add imports (or other code) to all templates using the <code>imports</code> argument:
297
<div class="highlight" ><pre><span class="n">t</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">'/tmp'</span><span class="p">],</span>
298
<span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">'unicode'</span><span class="p">,</span> <span class="s">'myfilter'</span><span class="p">],</span>
299
<span class="n">imports</span><span class="o">=</span><span class="p">[</span><span class="s">'from mypackage import myfilter'</span><span class="p">])</span>
303
<p>The above will generate templates something like this:
310
<div class="highlight" ><pre><span class="c"># ....</span>
311
<span class="k">from</span> <span class="nn">mypackage</span> <span class="k">import</span> <span class="n">myfilter</span>
313
<span class="k">def</span> <span class="nf">render_body</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
314
<span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">myfilter</span><span class="p">(</span><span class="nb">unicode</span><span class="p">(</span><span class="s">"some text"</span><span class="p">)))</span>
321
<a href="#top">back to section top</a>
327
<A name="filtering_expression_turning"></a>
329
<div class="subsection">
332
<h3>Turning off Filtering with the "n" filter</h3>
336
<p>In all cases the special <code>n</code> filter, used locally within an expression, will <strong>disable</strong> all filters declared in the <code><%page></code> tag as well <code>default_filters</code>. Such as:
343
<div class="highlight" ><pre><span class="cp">${</span><span class="s">'myexpression'</span> <span class="o">|</span> <span class="n">n</span><span class="cp">}</span>
347
<p>Will render <code>myexpression</code> with no filtering of any kind, and
354
<div class="highlight" ><pre><span class="cp">${</span><span class="s">'myexpression'</span> <span class="o">|</span> <span class="n">n</span><span class="p">,</span> <span class="n">trim</span><span class="cp">}</span>
358
<p>will render <code>myexpression</code> using the <code>trim</code> filter only. <br></br>
368
<a href="#top">back to section top</a>
374
<A name="filtering_filtering"></a>
376
<div class="subsection">
379
<h3>Filtering Defs</h3>
383
<p>The <code>%def</code> tag has a filter argument which will apply the given list of filter functions to the output of the <code>%def</code>:
390
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"foo()"</span> <span class="na">filter=</span><span class="s">"h, trim"</span><span class="cp">></span>
391
<span class="nt"><b></span>this is bold<span class="nt"></b></span>
392
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span>
396
<p>When the filter attribute is applied to a def as above, the def is automatically <strong>buffered</strong> as well. This is described next.
401
<a href="#top">back to section top</a>
407
<A name="filtering_buffering"></a>
409
<div class="subsection">
416
<p>One of Mako's central design goals is speed. To this end, all of the textual content within a template and its various callables is by default piped directly to the single buffer that is stored within the <code>Context</code> object. While this normally is easy to miss, it has certain side effects. The main one is that when you call a def using the normal expression syntax, i.e. <code>${somedef()}</code>, it may appear that the return value of the function is the content it produced, which is then delivered to your template just like any other expression substitution, except that normally, this is not the case; the return value of <code>${somedef()}</code> is simply the empty string <code>''</code>. By the time you receive this empty string, the output of <code>somedef()</code> has been sent to the underlying buffer.
418
<p>You may not want this effect, if for example you are doing something like this:
425
<div class="highlight" ><pre><span class="cp">${</span><span class="s">" results "</span> <span class="o">+</span> <span class="n">somedef</span><span class="p">()</span> <span class="o">+</span> <span class="s">" more results "</span><span class="cp">}</span>
429
<p>If the <code>somedef()</code> function produced the content "<code>somedef's results</code>", the above template would produce this output:
436
<div class="highlight" ><pre>somedef's results results more results
440
<p>This is because <code>somedef()</code> fully executes before the expression returns the results of its concatenation; the concatenation in turn receives just the empty string as its middle expression.
442
<p>Mako provides two ways to work around this. One is by applying buffering to the <code>%def</code> itself:
449
<div class="highlight" ><pre><span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"somedef()"</span> <span class="na">buffered=</span><span class="s">"true"</span><span class="cp">></span>
450
somedef's results
451
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span>
455
<p>The above definition will generate code similar to this:
462
<div class="highlight" ><pre>def somedef():
463
context.push_buffer()
465
context.write("somedef's results")
467
buf = context.pop_buffer()
468
return buf.getvalue()
472
<p>So that the content of <code>somedef()</code> is sent to a second buffer, which is then popped off the stack and its value returned. The speed hit inherent in buffering the output of a def is also apparent.
474
<p>Note that the <code>filter</code> argument on %def also causes the def to be buffered. This is so that the final content of the %def can be delivered to the escaping function in one batch, which reduces method calls and also produces more deterministic behavior for the filtering function itself, which can possibly be useful for a filtering function that wishes to apply a transformation to the text as a whole.
476
<p>The other way to buffer the output of a def or any Mako callable is by using the built-in <code>capture</code> function. This function performs an operation similar to the above buffering operation except it is specified by the caller.
483
<div class="highlight" ><pre><span class="cp">${</span><span class="s">" results "</span> <span class="o">+</span> <span class="n">capture</span><span class="p">(</span><span class="n">somedef</span><span class="p">)</span> <span class="o">+</span> <span class="s">" more results "</span><span class="cp">}</span>
487
<p>Note that the first argument to the <code>capture</code> function is <strong>the function itself</strong>, not the result of calling it. This is because the <code>capture</code> function takes over the job of actually calling the target function, after setting up a buffered environment. To send arguments to the function, just send them to <code>capture</code> instead:
494
<div class="highlight" ><pre><span class="cp">${</span><span class="n">capture</span><span class="p">(</span><span class="n">somedef</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="s">'hi'</span><span class="p">,</span> <span class="n">use_paging</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span><span class="cp">}</span>
498
<p>The above call is equivalent to the unbuffered call:
505
<div class="highlight" ><pre><span class="cp">${</span><span class="n">somedef</span><span class="p">(</span><span class="mi">17</span><span class="p">,</span> <span class="s">'hi'</span><span class="p">,</span> <span class="n">use_paging</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span><span class="cp">}</span>
518
<a href="#top">back to section top</a>
525
<div class="toolbar">
526
<div class="prevnext">
527
Previous: <a href="inheritance.html">Inheritance</a>
530
Next: <a href="unicode.html">The Unicode Chapter</a>
532
<h3><a href="index.html">Table of Contents</a></h3>