Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > os > by-pkgid > b72eba3c7c7da3672713b95f0a47d2a4 > files > 84

python-mako-0.3.4-2.fc14.noarch.rpm

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Mako Documentation - Filtering and Buffering</title>
	
    <link rel="stylesheet" href="docs.css"></link>
    <link rel="stylesheet" href="highlight.css"></link>
    




</head>
<body>










<div id="topanchor"><a name="top">&nbsp;</a></div>

<div id="pagecontrol"><a href="index.html">Multiple Pages</a> | <a href="documentation.html">One Page</a></div>

<h1>Mako Documentation</h1>

<div class="versionheader">Version: 0.3.4   Last Updated: 06/22/10 17:39:23</div>







<A name=""></a>


    <div class="topnav">

    
    <div class="toolbar">
        <div class="prevnext">
            Previous: <a href="inheritance.html">Inheritance</a>

            |
            Next: <a href="unicode.html">The Unicode Chapter</a>
        </div>
        <h3><a href="index.html">Table of Contents</a></h3>
    </div>


    <br/>
	<a href="#filtering">Filtering and Buffering</a>
	
	
    <ul>
        <li><A style="" href="filtering.html#filtering_expression">Expression Filtering</a></li>
        
            
    <ul>
        <li><A style="" href="filtering.html#filtering_expression_defaultfilters">The default_filters Argument</a></li>
        
            
    <ul>
    </ul>

        <li><A style="" href="filtering.html#filtering_expression_turning">Turning off Filtering with the "n" filter</a></li>
        
            
    <ul>
    </ul>

    </ul>

        <li><A style="" href="filtering.html#filtering_filtering">Filtering Defs</a></li>
        
            
    <ul>
    </ul>

        <li><A style="" href="filtering.html#filtering_buffering">Buffering</a></li>
        
            
    <ul>
    </ul>

        <li><A style="" href="filtering.html#filtering_decorating">Decorating</a></li>
        
            
    <ul>
    </ul>

    </ul>

	</div>











    
    <A name="filtering"></a>
    
    <div class="section">
    

    <h3>Filtering and Buffering</h3>
    
    



    
    <A name="filtering_expression"></a>
    
    <div class="subsection">
    

    <h3>Expression Filtering</h3>
    
    

<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:
</p>

    
    

    <div class="code">
        <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>
</pre></div>

    </div>
<p>The above expression applies URL escaping to the expression, and produces <code>this+is+some+text</code>.
</p>
<p>The built-in escape flags are:
</p>
<ul>
 <li>
     <code>u</code> : URL escaping, provided by <code>urllib.quote_plus(string.encode(&#39;utf-8&#39;))</code>
 </li>

 <li>
     <code>h</code> : HTML escaping, provided by <code>markupsafe.escape(string)</code>  (new as of 0.3.4 - prior versions use <code>cgi.escape(string, True)</code>)
 </li>

 <li>
     <code>x</code> : XML escaping
 </li>

 <li>
     <code>trim</code> : whitespace trimming, provided by <code>string.strip()</code>
 </li>

 <li>
     <code>entity</code> : produces HTML entity references for applicable strings, derived from <code>htmlentitydefs</code>
 </li>

 <li>
     <code>unicode</code> (<code>str</code> on Python 3): produces a Python unicode string (this function is applied by default).
 </li>

 <li>
     <code>decode.&lt;some encoding&gt;</code> : decode input into a Python unicode with the specified encoding
 </li>

 <li>
     <code>n</code> : 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="code">
        <div class="highlight"><pre><span class="cp">${</span><span class="s">&quot;  &lt;tag&gt;some value&lt;/tag&gt; &quot;</span> <span class="o">|</span> <span class="n">h</span><span class="p">,</span><span class="n">trim</span><span class="cp">}</span>
</pre></div>

    </div>
<p>The above produces <code>&amp;lt;tag&amp;gt;some value&amp;lt;/tag&amp;gt;</code>, 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 <code>|</code> operator draw upon the local namespace of the template in which they appear, meaning you can define escaping functions locally:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</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">&quot;&lt;TAG&gt;&quot;</span> <span class="o">+</span> <span class="n">text</span> <span class="o">+</span> <span class="s">&quot;&lt;/TAG&gt;&quot;</span>
<span class="cp">%&gt;</span>

Heres some tagged text: <span class="cp">${</span><span class="s">&quot;text&quot;</span> <span class="o">|</span> <span class="n">myescape</span><span class="cp">}</span>
</pre></div>

    </div>
<p>Or from any Python module:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</span>
    <span class="kn">import</span> <span class="nn">myfilters</span>
<span class="cp">%&gt;</span>

Heres some tagged text: <span class="cp">${</span><span class="s">&quot;text&quot;</span> <span class="o">|</span> <span class="n">myfilters</span><span class="o">.</span><span class="n">tagfilter</span><span class="cp">}</span>
</pre></div>

    </div>
<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:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">expression_filter=</span><span class="s">&quot;h&quot;</span><span class="cp">/&gt;</span>

Escaped text:  <span class="cp">${</span><span class="s">&quot;&lt;html&gt;some html&lt;/html&gt;&quot;</span><span class="cp">}</span>
</pre></div>

    </div>
<p>Result:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre>Escaped text: <span class="ni">&amp;lt;</span>html<span class="ni">&amp;gt;</span>some html<span class="ni">&amp;lt;</span>/html<span class="ni">&amp;gt;</span>
</pre></div>

    </div>


    
    <A name="filtering_expression_defaultfilters"></a>
    
    <div class="subsection">
    

    <h3>The default_filters Argument</h3>
    
    

<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, when given its default argument of <code>None</code>, will be internally set to <code>[&#34;unicode&#34;]</code> (or <code>[&#34;str&#34;]</code> on Python 3), except when <code>disable_unicode=True</code> is set in which case it defaults to <code>[&#34;str&#34;]</code>:
</p>

    
    

    <div class="code">
        <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">&#39;/tmp&#39;</span><span class="p">],</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;unicode&#39;</span><span class="p">])</span>
</pre></div>

    </div>
<p>To replace the usual <code>unicode</code>/<code>str</code> function with a specific encoding, the <code>decode</code> filter can be substituted:
</p>

    
    

    <div class="code">
        <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">&#39;/tmp&#39;</span><span class="p">],</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;decode.utf8&#39;</span><span class="p">])</span>
</pre></div>

    </div>
<p>To disable <code>default_filters</code> entirely, set it to an empty list:
</p>

    
    

    <div class="code">
        <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">&#39;/tmp&#39;</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 <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.
</p>

    
    

    <div class="code">
        <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">&#39;unicode&#39;</span><span class="p">,</span> <span class="s">&#39;myfilter&#39;</span><span class="p">])</span>
</pre></div>

    </div>
<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:
</p>

    
    

    <div class="code">
        <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">&#39;/tmp&#39;</span><span class="p">],</span> 
    <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;unicode&#39;</span><span class="p">,</span> <span class="s">&#39;myfilter&#39;</span><span class="p">],</span> 
    <span class="n">imports</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;from mypackage import myfilter&#39;</span><span class="p">])</span>
</pre></div>

    </div>
<p>The above will generate templates something like this:
</p>

    
    

    <div class="code">
        <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">&quot;some text&quot;</span><span class="p">)))</span>
</pre></div>

    </div>



            <a href="#top">back to section top</a>
    </div>



    
    <A name="filtering_expression_turning"></a>
    
    <div class="subsection">
    

    <h3>Turning off Filtering with the "n" filter</h3>
    
    

<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>&lt;%page&gt;</code> tag as well <code>default_filters</code>.  Such as:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">${</span><span class="s">&#39;myexpression&#39;</span> <span class="o">|</span> <span class="n">n</span><span class="cp">}</span>
</pre></div>

    </div>
<p>Will render <code>myexpression</code> with no filtering of any kind, and
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">${</span><span class="s">&#39;myexpression&#39;</span> <span class="o">|</span> <span class="n">n</span><span class="p">,</span> <span class="n">trim</span><span class="cp">}</span>
</pre></div>

    </div>
<p>will render <code>myexpression</code> using the <code>trim</code> filter only.  <br></br>
</p>



    </div>




            <a href="#top">back to section top</a>
    </div>



    
    <A name="filtering_filtering"></a>
    
    <div class="subsection">
    

    <h3>Filtering Defs</h3>
    
    

<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>:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><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">filter=</span><span class="s">&quot;h, trim&quot;</span><span class="cp">&gt;</span>
    <span class="nt">&lt;b&gt;</span>this is bold<span class="nt">&lt;/b&gt;</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</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>



            <a href="#top">back to section top</a>
    </div>



    
    <A name="filtering_buffering"></a>
    
    <div class="subsection">
    

    <h3>Buffering</h3>
    
    

<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>&#39;&#39;</code>.  By the time you receive this empty string, the output of <code>somedef()</code> 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="code">
        <div class="highlight"><pre><span class="cp">${</span><span class="s">&quot; results &quot;</span> <span class="o">+</span> <span class="n">somedef</span><span class="p">()</span> <span class="o">+</span> <span class="s">&quot; more results &quot;</span><span class="cp">}</span>
</pre></div>

    </div>
<p>If the <code>somedef()</code> function produced the content "<code>somedef&#39;s results</code>", the above template would produce this output:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre>somedef&#39;s results results more results
</pre></div>

    </div>
<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.
</p>
<p>Mako provides two ways to work around this.  One is by applying buffering to the <code>%def</code> itself:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;somedef()&quot;</span> <span class="na">buffered=</span><span class="s">&quot;True&quot;</span><span class="cp">&gt;</span>
    somedef&#39;s results
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>
</pre></div>

    </div>
<p>The above definition will generate code similar to this:
</p>

    
    

    <div class="code">
        <div class="highlight"><pre>def somedef():
    context.push_buffer()
    try:
        context.write(&quot;somedef&#39;s results&quot;)
    finally:
        buf = context.pop_buffer()
    return buf.getvalue()
</pre></div>

    </div>
<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.
</p>
<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.
</p>
<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.
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">${</span><span class="s">&quot; results &quot;</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">&quot; more results &quot;</span><span class="cp">}</span>
</pre></div>

    </div>
<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:
</p>

    
    

    <div class="code">
        <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">&#39;hi&#39;</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>
</pre></div>

    </div>
<p>The above call is equivalent to the unbuffered call:
</p>

    
    

    <div class="code">
        <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">&#39;hi&#39;</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>
</pre></div>

    </div>



            <a href="#top">back to section top</a>
    </div>



    
    <A name="filtering_decorating"></a>
    
    <div class="subsection">
    

    <h3>Decorating</h3>
    
    

<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 <code>decorator</code> argument to <code>%def</code> 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><code>decorator</code> 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="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</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">&quot;BAR&quot;</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">&quot;BAR&quot;</span><span class="p">)</span>
            <span class="k">return</span> <span class="s">&#39;&#39;</span>
        <span class="k">return</span> <span class="n">decorate</span>
<span class="cp">%&gt;</span>

<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">decorator=</span><span class="s">&quot;bar&quot;</span><span class="cp">&gt;</span>
    this is foo
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

<span class="cp">${</span><span class="n">foo</span><span class="p">()</span><span class="cp">}</span>
</pre></div>

    </div>
<p>The above template will return, with more whitespace than this, <code>&#34;BAR this is foo BAR&#34;</code>.  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 <code>capture</code> callable in the <code>mako.runtime</code> module (available in templates as just  <code>runtime</code>):
</p>

    
    

    <div class="code">
        <div class="highlight"><pre><span class="cp">&lt;%!</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">&quot;BAR&quot;</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">&quot;BAR&quot;</span>
        <span class="k">return</span> <span class="n">decorate</span>
<span class="cp">%&gt;</span>

<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">decorator=</span><span class="s">&quot;bar&quot;</span><span class="cp">&gt;</span>
    this is foo
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span>

<span class="cp">${</span><span class="n">foo</span><span class="p">()</span><span class="cp">}</span>
</pre></div>

    </div>
<p>The decorator can be used with top-level defs as well as nested defs.  Note that when calling a top-level def from the <code>Template</code> api, i.e. <code>template.get_def(&#39;somedef&#39;).render()</code>, the decorator has to write the output to the <code>context</code>, i.e. as in the first example.  The return value gets discarded.
</p>




    </div>




            <a href="#top">back to section top</a>
    </div>


</html>


    <div class="toolbar">
        <div class="prevnext">
            Previous: <a href="inheritance.html">Inheritance</a>

            |
            Next: <a href="unicode.html">The Unicode Chapter</a>
        </div>
        <h3><a href="index.html">Table of Contents</a></h3>
    </div>






</body>
</html>