Syntax:
#filter FILTER_CLASS_NAME #filter $PLACEHOLDER_TO_A_FILTER_INSTANCE #filter None
Output from $placeholders is passed through an ouput filter. The default
filter merely returns a string representation of the placeholder value,
unless the value is None
, in which case the filter returns an empty
string. Only top-level placeholders invoke the filter; placeholders inside
expressions do not.
Certain filters take optional arguments to modify their behaviour. To pass
arguments, use the long placeholder syntax and precede each filter argument by
a comma. By convention, filter arguments don't take a $
prefix, to
avoid clutter in the placeholder tag which already has plenty of dollar signs.
For instance, the MaxLen filter takes an argument 'maxlen':
${placeholderName, maxlen=20} ${functionCall($functionArg), maxlen=$myMaxLen}
To change the output filter, use the 'filter'
keyword to the
Template
class constructor, or the #filter
directive at runtime (details below). You may use #filter
as often as
you wish to switch between several filters, if certain $placeholders
need one filter and other $placeholders
need another.
The standard filters are in the module Cheetah.Filters
. Cheetah
currently provides:
Filter
str(whateverItIs)
. This is the base class for all other filters,
and the minimum behaviour for all filters distributed with Cheetah.
ReplaceNone
MaxLen
Pager
WebSafe
The filter argument 'also' may be used to specify additional characters to escape. For instance, say you want to ensure a value displays all on one line. Escape all spaces in the value with ' ', the non-breaking space:
${$country, also=' '}}
To switch filters using a class object, pass the class using the
filter argument to the Template constructor, or via a placeholder to the
#filter
directive: #filter $myFilterClass
. The class must be
a subclass of Cheetah.Filters.Filter
. When passing a class object, the
value of filtersLib does not matter, and it does not matter where the
class was defined.
To switch filters by name, pass the name of the class as a string using the
filter argument to the Template constructor, or as a bare word (without
quotes) to the #filter
directive: #filter TheFilter
. The
class will be looked up in the filtersLib.
The filtersLib is a module containing filter classes, by default
Cheetah.Filters
. All classes in the module that are subclasses of
Cheetah.Filters.Filter
are considered filters. If your filters are in
another module, pass the module object as the filtersLib argument to the
Template constructor.
Writing a custom filter is easy: just override the .filter
method.
def filter(self, val, **kw): # Returns a string.
None
. Cheetah passes one keyword
argument: kw['rawExpr']
is the placeholder name as it appears in
the template definition, including all subscripts and arguments. If you use
the long placeholder syntax, any options you pass appear as keyword
arguments. Again, the return value must be a string.
You can always switch back to the default filter this way:
#filter None
. This is easy to remember because "no filter" means the
default filter, and because None happens to be the only object the default
filter treats specially.
We are considering additional filters; see http://webware.colorstudy.net/twiki/bin/view/Cheetah/MoreFiltersfor the latest ideas.