============ Filter Howto ============ You can easily add your own filters to the Jinja filter system. Filters are just Python functions that takes at least on arguments, the value of the variable (input) -- not necessarily a string. And additonally an unlimted number of arguments -- these can have a default value. For example in the filter ``{{ var | foobar "spam", "eggs" }}`` the filter ``foobar`` would be passed the variables ``spam`` and ``eggs`` als the arguments. 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. Here is an example for a small string filter definition:: 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) 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:: def do_sum(iterable, context, max=None): res = sum(iterable) if not max is None and res > max: res = max return unicode(res) When you've written your filter definition you have to register it in the standard library:: from jinja.library import stdlib stdlib.register_filter('ljust', handle_ljust) stdlib.register_filter('sum', handle_sum) If you want your own lib to not affect the default library you can use the ``clone`` method:: from jinja.library import stdlib mylib = stdlib.clone() mylib.register_filter('ljust', handle_ljust) mylib.register_filter('sum', handle_sum) The constructor of the ``Template`` class takes a ``lib`` tag for defining the library the template should use. All you have to do is to ensure that the module gets loaded before Jinja starts the rendering process. Then the filter ``ljust`` will be available in the list of known filters:: This is a small quote: {{ quote | ljust 10 }}