/usr/share/doc/python-mako-doc/html/filtering.html is in python-mako-doc 1.0.0+dfsg-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | <html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<head>
<title>
Filtering and Buffering
—
Mako 1.0.0 Documentation
</title>
<!-- begin iterate through sphinx environment css_files -->
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/docs.css" type="text/css" />
<link rel="stylesheet" href="_static/site.css" type="text/css" />
<link rel="stylesheet" href="_static/changelog.css" type="text/css" />
<link rel="stylesheet" href="_static/sphinx_paramlinks.css" type="text/css" />
<!-- end iterate through sphinx environment css_files -->
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0.0',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="Mako 1.0.0 Documentation" href="index.html" />
<link rel="next" title="The Unicode Chapter" href="unicode.html" />
<link rel="prev" title="Inheritance" href="inheritance.html" />
</head>
<body>
<div id="wrap">
<div class="rightbar">
<div class="slogan">
Hyperfast and lightweight templating for the Python platform.
</div>
</div>
<a href="http://www.makotemplates.org/"><img src="_static/makoLogo.png" /></a>
<hr/>
<div id="docs-container">
<div id="docs-header">
<h1>Mako 1.0.0 Documentation</h1>
<div id="docs-search">
Search:
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" /> <input type="submit" value="Search" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<div id="docs-version-header">
Release: <span class="version-num">1.0.0</span>
</div>
</div>
<div id="docs-top-navigation">
<div id="docs-top-page-control" class="docs-navigation-links">
<ul>
<li>Prev:
<a href="inheritance.html" title="previous chapter">Inheritance</a>
</li>
<li>Next:
<a href="unicode.html" title="next chapter">The Unicode Chapter</a>
</li>
<li>
<a href="index.html">Table of Contents</a> |
<a href="genindex.html">Index</a>
| <a href="_sources/filtering.txt">view source
</li>
</ul>
</div>
<div id="docs-navigation-banner">
<a href="index.html">Mako 1.0.0 Documentation</a>
»
Filtering and Buffering
<h2>
Filtering and Buffering
</h2>
</div>
</div>
<div id="docs-body-container">
<div id="docs-sidebar">
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Filtering and Buffering</a><ul>
<li><a class="reference internal" href="#expression-filtering">Expression Filtering</a><ul>
<li><a class="reference internal" href="#the-default-filters-argument">The <tt class="docutils literal"><span class="pre">default_filters</span></tt> Argument</a></li>
<li><a class="reference internal" href="#turning-off-filtering-with-the-n-filter">Turning off Filtering with the <tt class="docutils literal"><span class="pre">n</span></tt> Filter</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filtering-defs-and-blocks">Filtering Defs and Blocks</a></li>
<li><a class="reference internal" href="#buffering">Buffering</a></li>
<li><a class="reference internal" href="#decorating">Decorating</a></li>
</ul>
</li>
</ul>
<h4>Previous Topic</h4>
<p>
<a href="inheritance.html" title="previous chapter">Inheritance</a>
</p>
<h4>Next Topic</h4>
<p>
<a href="unicode.html" title="next chapter">The Unicode Chapter</a>
</p>
<h4>Quick Search</h4>
<p>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" /> <input type="submit" value="Search" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</p>
</div>
<div id="docs-body" class="withsidebar" >
<div class="section" id="filtering-and-buffering">
<span id="filtering-toplevel"></span><h1>Filtering and Buffering<a class="headerlink" href="#filtering-and-buffering" title="Permalink to this headline">¶</a></h1>
<div class="section" id="expression-filtering">
<h2>Expression Filtering<a class="headerlink" href="#expression-filtering" title="Permalink to this headline">¶</a></h2>
<p>As described in the chapter <a class="reference internal" href="syntax.html"><em>Syntax</em></a>, the “<tt class="docutils literal"><span class="pre">|</span></tt>” operator can be
applied to a “<tt class="docutils literal"><span class="pre">${}</span></tt>” expression to apply escape filters to the
output:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>The above expression applies URL escaping to the expression, and
produces <tt class="docutils literal"><span class="pre">this+is+some+text</span></tt>.</p>
<p>The built-in escape flags are:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">u</span></tt> : URL escaping, provided by
<tt class="docutils literal"><span class="pre">urllib.quote_plus(string.encode('utf-8'))</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">h</span></tt> : HTML escaping, provided by
<tt class="docutils literal"><span class="pre">markupsafe.escape(string)</span></tt></p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.3.4: </span>Prior versions use <tt class="docutils literal"><span class="pre">cgi.escape(string,</span> <span class="pre">True)</span></tt>.</p>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">x</span></tt> : XML escaping</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">trim</span></tt> : whitespace trimming, provided by <tt class="docutils literal"><span class="pre">string.strip()</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">entity</span></tt> : produces HTML entity references for applicable
strings, derived from <tt class="docutils literal"><span class="pre">htmlentitydefs</span></tt></p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">unicode</span></tt> (<tt class="docutils literal"><span class="pre">str</span></tt> on Python 3): produces a Python unicode
string (this function is applied by default)</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">decode.<some</span> <span class="pre">encoding></span></tt>: decode input into a Python
unicode with the specified encoding</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">n</span></tt> : disable all default filtering; only filters specified
in the local expression tag will be applied.</p>
</li>
</ul>
<p>To apply more than one filter, separate them by a comma:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>The above produces <tt class="docutils literal"><span class="pre">&lt;tag&gt;some</span> <span class="pre">value&lt;/tag&gt;</span></tt>, with
no leading or trailing whitespace. The HTML escaping function is
applied first, the “trim” function second.</p>
<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 <tt class="docutils literal"><span class="pre">|</span></tt>
operator draw upon the local namespace of the template in which
they appear, meaning you can define escaping functions locally:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span>
<span class="k">def</span> <span class="nf">myescape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
<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>
<span class="cp">%></span><span class="x"></span>
<span class="x">Here's some tagged text: </span><span class="cp">${</span><span class="s">"text"</span> <span class="o">|</span> <span class="n">myescape</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>Or from any Python module:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span>
<span class="kn">import</span> <span class="nn">myfilters</span>
<span class="cp">%></span><span class="x"></span>
<span class="x">Here's some tagged text: </span><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><span class="x"></span>
</pre></div>
</div>
<p>A page can apply a default set of filters to all expression tags
using the <tt class="docutils literal"><span class="pre">expression_filter</span></tt> argument to the <tt class="docutils literal"><span class="pre">%page</span></tt> tag:</p>
<div class="highlight-mako"><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><span class="x"></span>
<span class="x">Escaped text: </span><span class="cp">${</span><span class="s">"<html>some html</html>"</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>Result:</p>
<div class="highlight-html"><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>
</pre></div>
</div>
<div class="section" id="the-default-filters-argument">
<span id="filtering-default-filters"></span><h3>The <tt class="docutils literal"><span class="pre">default_filters</span></tt> Argument<a class="headerlink" href="#the-default-filters-argument" title="Permalink to this headline">¶</a></h3>
<p>In addition to the <tt class="docutils literal"><span class="pre">expression_filter</span></tt> argument, the
<tt class="docutils literal"><span class="pre">default_filters</span></tt> argument to both <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> and
<a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> can specify filtering for all expression tags
at the programmatic level. This array-based argument, when given
its default argument of <tt class="docutils literal"><span class="pre">None</span></tt>, will be internally set to
<tt class="docutils literal"><span class="pre">["unicode"]</span></tt> (or <tt class="docutils literal"><span class="pre">["str"]</span></tt> on Python 3), except when
<tt class="docutils literal"><span class="pre">disable_unicode=True</span></tt> is set in which case it defaults to
<tt class="docutils literal"><span class="pre">["str"]</span></tt>:</p>
<div class="highlight-python"><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>
</pre></div>
</div>
<p>To replace the usual <tt class="docutils literal"><span class="pre">unicode</span></tt>/<tt class="docutils literal"><span class="pre">str</span></tt> function with a
specific encoding, the <tt class="docutils literal"><span class="pre">decode</span></tt> filter can be substituted:</p>
<div class="highlight-python"><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>
</pre></div>
</div>
<p>To disable <tt class="docutils literal"><span class="pre">default_filters</span></tt> entirely, set it to an empty
list:</p>
<div class="highlight-python"><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>
</pre></div>
</div>
<p>Any string name can be added to <tt class="docutils literal"><span class="pre">default_filters</span></tt> 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.</p>
<div class="highlight-python"><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>
</pre></div>
</div>
<p>To ease the usage of <tt class="docutils literal"><span class="pre">default_filters</span></tt> with custom filters,
you can also add imports (or other code) to all templates using
the <tt class="docutils literal"><span class="pre">imports</span></tt> argument:</p>
<div class="highlight-python"><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> <span class="s">'myfilter'</span><span class="p">],</span>
<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>
</pre></div>
</div>
<p>The above will generate templates something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># ....</span>
<span class="kn">from</span> <span class="nn">mypackage</span> <span class="kn">import</span> <span class="n">myfilter</span>
<span class="k">def</span> <span class="nf">render_body</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
<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>
</pre></div>
</div>
</div>
<div class="section" id="turning-off-filtering-with-the-n-filter">
<h3>Turning off Filtering with the <tt class="docutils literal"><span class="pre">n</span></tt> Filter<a class="headerlink" href="#turning-off-filtering-with-the-n-filter" title="Permalink to this headline">¶</a></h3>
<p>In all cases the special <tt class="docutils literal"><span class="pre">n</span></tt> filter, used locally within an
expression, will <strong>disable</strong> all filters declared in the
<tt class="docutils literal"><span class="pre"><%page></span></tt> tag as well as in <tt class="docutils literal"><span class="pre">default_filters</span></tt>. Such as:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>will render <tt class="docutils literal"><span class="pre">myexpression</span></tt> with no filtering of any kind, and:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>will render <tt class="docutils literal"><span class="pre">myexpression</span></tt> using the <tt class="docutils literal"><span class="pre">trim</span></tt> filter only.</p>
</div>
</div>
<div class="section" id="filtering-defs-and-blocks">
<h2>Filtering Defs and Blocks<a class="headerlink" href="#filtering-defs-and-blocks" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">%def</span></tt> and <tt class="docutils literal"><span class="pre">%block</span></tt> tags have an argument called <tt class="docutils literal"><span class="pre">filter</span></tt> which will apply the
given list of filter functions to the output of the <tt class="docutils literal"><span class="pre">%def</span></tt>:</p>
<div class="highlight-mako"><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><span class="x"></span>
<span class="x"> <b>this is bold</b></span>
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span>
</pre></div>
</div>
<p>When the <tt class="docutils literal"><span class="pre">filter</span></tt> attribute is applied to a def as above, the def
is automatically <strong>buffered</strong> as well. This is described next.</p>
</div>
<div class="section" id="buffering">
<h2>Buffering<a class="headerlink" href="#buffering" title="Permalink to this headline">¶</a></h2>
<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 <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> 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.
<tt class="docutils literal"><span class="pre">${somedef()}</span></tt>, 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
<tt class="docutils literal"><span class="pre">${somedef()}</span></tt> is simply the empty string <tt class="docutils literal"><span class="pre">''</span></tt>. By the time
you receive this empty string, the output of <tt class="docutils literal"><span class="pre">somedef()</span></tt> has
been sent to the underlying buffer.</p>
<p>You may not want this effect, if for example you are doing
something like this:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>If the <tt class="docutils literal"><span class="pre">somedef()</span></tt> function produced the content “<tt class="docutils literal"><span class="pre">somedef's</span>
<span class="pre">results</span></tt>”, the above template would produce this output:</p>
<div class="highlight-html"><div class="highlight"><pre>somedef's results results more results
</pre></div>
</div>
<p>This is because <tt class="docutils literal"><span class="pre">somedef()</span></tt> fully executes before the
expression returns the results of its concatenation; the
concatenation in turn receives just the empty string as its
middle expression.</p>
<p>Mako provides two ways to work around this. One is by applying
buffering to the <tt class="docutils literal"><span class="pre">%def</span></tt> itself:</p>
<div class="highlight-mako"><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><span class="x"></span>
<span class="x"> somedef's results</span>
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span>
</pre></div>
</div>
<p>The above definition will generate code similar to this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">somedef</span><span class="p">():</span>
<span class="n">context</span><span class="o">.</span><span class="n">push_buffer</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"somedef's results"</span><span class="p">)</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">buf</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">pop_buffer</span><span class="p">()</span>
<span class="k">return</span> <span class="n">buf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
</pre></div>
</div>
<p>So that the content of <tt class="docutils literal"><span class="pre">somedef()</span></tt> 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.</p>
<p>Note that the <tt class="docutils literal"><span class="pre">filter</span></tt> argument on <tt class="docutils literal"><span class="pre">%def</span></tt> also causes the def to
be buffered. This is so that the final content of the <tt class="docutils literal"><span class="pre">%def</span></tt> 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.</p>
<p>The other way to buffer the output of a def or any Mako callable
is by using the built-in <tt class="docutils literal"><span class="pre">capture</span></tt> function. This function
performs an operation similar to the above buffering operation
except it is specified by the caller.</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>Note that the first argument to the <tt class="docutils literal"><span class="pre">capture</span></tt> function is
<strong>the function itself</strong>, not the result of calling it. This is
because the <tt class="docutils literal"><span class="pre">capture</span></tt> 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 <tt class="docutils literal"><span class="pre">capture</span></tt> instead:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
<p>The above call is equivalent to the unbuffered call:</p>
<div class="highlight-mako"><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><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="decorating">
<h2>Decorating<a class="headerlink" href="#decorating" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.2.5.</span></p>
</div>
<p>Somewhat like a filter for a <tt class="docutils literal"><span class="pre">%def</span></tt> but more flexible, the <tt class="docutils literal"><span class="pre">decorator</span></tt>
argument to <tt class="docutils literal"><span class="pre">%def</span></tt> allows the creation of a function that will
work in a similar manner to a Python decorator. The function can
control whether or not the function executes. The original
intent of this function is to allow the creation of custom cache
logic, but there may be other uses as well.</p>
<p><tt class="docutils literal"><span class="pre">decorator</span></tt> is intended to be used with a regular Python
function, such as one defined in a library module. Here we’ll
illustrate the python function defined in the template for
simplicities’ sake:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span>
<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">decorate</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"BAR"</span><span class="p">)</span>
<span class="n">fn</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
<span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"BAR"</span><span class="p">)</span>
<span class="k">return</span> <span class="s">''</span>
<span class="k">return</span> <span class="n">decorate</span>
<span class="cp">%></span><span class="x"></span>
<span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"foo()"</span> <span class="na">decorator=</span><span class="s">"bar"</span><span class="cp">></span><span class="x"></span>
<span class="x"> this is foo</span>
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span>
<span class="cp">${</span><span class="n">foo</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>The above template will return, with more whitespace than this,
<tt class="docutils literal"><span class="pre">"BAR</span> <span class="pre">this</span> <span class="pre">is</span> <span class="pre">foo</span> <span class="pre">BAR"</span></tt>. The function is the render callable
itself (or possibly a wrapper around it), and by default will
write to the context. To capture its output, use the <a class="reference internal" href="namespaces.html#mako.runtime.capture" title="mako.runtime.capture"><tt class="xref py py-func docutils literal"><span class="pre">capture()</span></tt></a>
callable in the <tt class="docutils literal"><span class="pre">mako.runtime</span></tt> module (available in templates
as just <tt class="docutils literal"><span class="pre">runtime</span></tt>):</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp"><%!</span>
<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">fn</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">decorate</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"BAR"</span> <span class="o">+</span> <span class="n">runtime</span><span class="o">.</span><span class="n">capture</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">fn</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span> <span class="o">+</span> <span class="s">"BAR"</span>
<span class="k">return</span> <span class="n">decorate</span>
<span class="cp">%></span><span class="x"></span>
<span class="cp"><%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">"foo()"</span> <span class="na">decorator=</span><span class="s">"bar"</span><span class="cp">></span><span class="x"></span>
<span class="x"> this is foo</span>
<span class="cp"></%</span><span class="nb">def</span><span class="cp">></span><span class="x"></span>
<span class="cp">${</span><span class="n">foo</span><span class="p">()</span><span class="cp">}</span><span class="x"></span>
</pre></div>
</div>
<p>The decorator can be used with top-level defs as well as nested
defs, and blocks too. Note that when calling a top-level def from the
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> API, i.e. <tt class="docutils literal"><span class="pre">template.get_def('somedef').render()</span></tt>,
the decorator has to write the output to the <tt class="docutils literal"><span class="pre">context</span></tt>, i.e.
as in the first example. The return value gets discarded.</p>
</div>
</div>
</div>
</div>
<div id="docs-bottom-navigation" class="docs-navigation-links">
Previous:
<a href="inheritance.html" title="previous chapter">Inheritance</a>
Next:
<a href="unicode.html" title="next chapter">The Unicode Chapter</a>
<div id="docs-copyright">
© Copyright the Mako authors and contributors.
Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3
with Mako templates.
</div>
</div>
</div>
<div class="clearfix">
<hr/>
<div class="copyright">Website content copyright © by Michael Bayer.
All rights reserved. Mako and its documentation are licensed
under the MIT license. mike(&)zzzcomputing.com</div>
</div>
</div>
</body>
</html>
|