<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Filtering and Buffering — Mako 0.4.2 Documentation</title> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="_static/docs.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '#', VERSION: '0.4.2', 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> <script type="text/javascript" src="_static/init.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="top" title="Mako 0.4.2 Documentation" href="index.html" /> <link rel="next" title="The Unicode Chapter" href="unicode.html" /> <link rel="prev" title="Inheritance" href="inheritance.html" /> </head> <body> <h1>Mako 0.4.2 Documentation</h1> <div id="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 class="versionheader"> Version: <span class="versionnum">0.4.2</span> Last Updated: 08/05/2011 17:45:59 </div> <div class="clearboth"></div> <div class="topnav"> <div id="pagecontrol"> <a href="genindex.html">Index</a> <div class="sourcelink">(<a href="_sources/filtering.txt">view source)</div> </div> <div class="navbanner"> <a class="totoc" href="index.html">Table of Contents</a> » Filtering and Buffering <div class="prevnext"> Previous: <a href="inheritance.html" title="previous chapter">Inheritance</a> Next: <a href="unicode.html" title="next chapter">The Unicode Chapter</a> </div> <h2> Filtering and Buffering </h2> </div> <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 default_filters Argument</a></li> <li><a class="reference internal" href="#turning-off-filtering-with-the-n-filter">Turning off Filtering with the “n” 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> <div class="clearboth"></div> </div> <div class="document"> <div class="body"> <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 class="simple"> <li><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></li> <li><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> (new as of 0.3.4 - prior versions use <tt class="docutils literal"><span class="pre">cgi.escape(string,</span> <span class="pre">True)</span></tt>)</li> <li><tt class="docutils literal"><span class="pre">x</span></tt> : XML escaping</li> <li><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></li> <li><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></li> <li><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).</li> <li><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</li> <li><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.</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">Heres 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">Heres 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 default_filters 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 <tt class="docutils literal"><span class="pre">Template</span></tt> and <tt class="docutils literal"><span class="pre">TemplateLookup</span></tt> can specify filtering for all expression tags at the programmatic level. This array-based argument, when given its default argument of <tt class="xref 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 “n” 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 <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 filter 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 <tt class="docutils literal"><span class="pre">Context</span></tt> 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 %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.</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> <p>This is a feature that’s new as of version 0.2.5. Somewhat like a filter for a %def 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 <tt class="docutils literal"><span class="pre">capture</span></tt> 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 <tt class="docutils literal"><span class="pre">Template</span></tt> 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 class="bottomnav"> <div class="prevnext"> Previous: <a href="inheritance.html" title="previous chapter">Inheritance</a> Next: <a href="unicode.html" title="next chapter">The Unicode Chapter</a> </div> <div class="doc_copyright"> © Copyright the Mako authors and contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </div> </body> </html>