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
|
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Filter Howto | Jinja Documentation</title>
<style text="text/css">
body {
font-family: 'Arial', sans-serif;
margin: 1em;
padding: 0;
}
#navigation {
float: right;
margin: -1em -1em 1em 1em;
padding: 1em 2em 0 2em;
border: 1px solid #bbb;
border-width: 0 0 1px 1px;
background-color: #f8f8f8;
}
#page {
width: 45em;
}
a {
color: #d00;
}
a:hover {
color: #d40;
}
h1 {
font-size: 2em;
color: #d00;
margin: 0.5em 0 0.5em 0;
padding: 0;
}
h2 {
font-size: 1.7em;
color: #bd2121;
margin: 1em 0 0.5em 0;
}
h3 {
font-size: 1.3em;
color: #8a2424;
margin: 0.5em 0 0 0;
}
p {
margin: 0.5em 1em 0.5em 1em;
}
pre {
margin: 1em 0 1em 2em;
padding: 0.5em;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f2f2f2;
overflow: auto;
}
li {
line-height: 1.4em;
}
hr {
margin: 1em;
padding: 0;
height: 1px!important;
background-color: #ccc;
border: none!important;
}
div.admonition {
margin: 1em 0 1em 1.5em;
padding: 0.5em 0.5em 0.5em 2em;
background-color: #f6e3e3;
border: 1px solid #d50000;
border-left: none;
border-right: none;
}
div.admonition p.admonition-title {
font-size: 1.1em;
color: #d40;
font-weight: bold;
margin: 0 0 0.5em -1em;
}
table {
border-collapse: collapse;
margin: 1em 2em 1em 1.5em;
}
table td, table th {
text-align: left;
border: 1px solid #eee;
padding: 0.3em;
}
table th {
background-color: #d00000;
color: white;
border: 1px solid #d00000;
border-bottom: 1px solid #eee;
}
</style>
</head>
<body>
<div id="navigation">
<h3>Documentation</h3>
<ul>
<li><a href="index.html">back to index</a></li>
<li><a href="http://wsgiarea.pocoo.org/repos/jinja/trunk/docs/source/filter-dev.txt">view source online</a></li>
</ul>
</div>
<h1>Filter Howto</h1>
<div id="page">
<p>You can easily add your own filters to the Jinja filter system.</p>
<p>Filters are just Python functions that takes at least on arguments, the value
of the variable (input) -- not necessarily a string.</p>
<p>And additonally an unlimted number of arguments -- these can have a default
value.</p>
<p>For example in the filter <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">var</span> <span class="pre">|</span> <span class="pre">foobar</span> <span class="pre">"spam",</span> <span class="pre">"eggs"</span> <span class="pre">}}</span></tt> the filter
<tt class="docutils literal"><span class="pre">foobar</span></tt> would be passed the variables <tt class="docutils literal"><span class="pre">spam</span></tt> and <tt class="docutils literal"><span class="pre">eggs</span></tt> als the
arguments.</p>
<p>Filter functions should always return something. They shouldn't raise
exceptions. They should fail silently. In case of error, they should return
either the original input or an empty string -- whichever makes more sense.</p>
<p>Here is an example for a small string filter definition:</p>
<pre class="literal-block">
from jinja.filters import stringfilter
def handle_ljust(s, width):
if not isinstance(s, basestring):
s = str(s)
return s.ljust(width)
handle_jlust = stringfilter(handle_ljust)
</pre>
<p>The decorator helps you to automagically do some charset related manipulation
on the input data. If you don't work on string data you have to do that on
your own. Here a normal definition:</p>
<pre class="literal-block">
def do_sum(iterable, context, max=None):
res = sum(iterable)
if not max is None and res > max:
res = max
return unicode(res)
</pre>
<p>When you've written your filter definition you have to register it in
the standard library:</p>
<pre class="literal-block">
from jinja.library import stdlib
stdlib.register_filter('ljust', handle_ljust)
stdlib.register_filter('sum', handle_sum)
</pre>
<p>If you want your own lib to not affect the default library you can use the
<tt class="docutils literal"><span class="pre">clone</span></tt> method:</p>
<pre class="literal-block">
from jinja.library import stdlib
mylib = stdlib.clone()
mylib.register_filter('ljust', handle_ljust)
mylib.register_filter('sum', handle_sum)
</pre>
<p>The constructor of the <tt class="docutils literal"><span class="pre">Template</span></tt> class takes a <tt class="docutils literal"><span class="pre">lib</span></tt> tag for defining
the library the template should use.</p>
<p>All you have to do is to ensure that the module gets loaded before Jinja starts
the rendering process. Then the filter <tt class="docutils literal"><span class="pre">ljust</span></tt> will be available in the list
of known filters:</p>
<pre class="literal-block">
This is a small quote:
{{ quote | ljust 10 }}
</pre>
</div>
</body>
</html>
|