Skip to content
Myr

[svn] added a snippet to the jinja docs for using django filters in jinja

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent ccf284b · Browse files · 2 files+41 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+41 added−1 removed

  • other:+41 added−1 removed

2 files+41 added−1 removed

modified:docs/src/devrecipies.txt+37 added-1 removed
2 unmodified lines
3=======================45Here some recipies for application developers.677Automagic Template Variables8============================910Python allows some magic stack manipulation which can be used to
24 unmodified lines
35 foo = "blub"36 return env.autorender('foo.html')3738In the template you can now access the local variables `seq` and `foo`.3940Using Django Filters with Jinja41===============================4243If you use Jinja in django and want to use some of the filters that44are part of the django core you can use this snippet:4546.. sourcecode:: python4748 def convert_django_filter(f):49 def filter_factory(*args):50 def wrapped(env, ctx, value):51 return f(value, *args)52 return wrapped53 return filter_factory5455You can now convert django filters for jinja using `convert_filter`. *Note*:56Django only supports one filter argument. Because of this limitation you57shouldn't pass it more arguments than it accepts. Because django uses some58introspection to find out if a filter accepts an argument weird things can59happen if you call it with an incompatible argument count.6061You can now register django filters for a jinja environment:6263.. sourcecode:: python6465 from django.template.defaultfilters import date66 env.filters['date'] = convert_django_filter(date)6768And use it:6970.. sourcecode:: jinja7172 {{ entry.pub_date|date }}7374Also keep in mind that Jinja knows about keywords, thus you cannot have a filter75that is called `pluralize` for example.
modified:docs/src/filters.txt+4 added
52 unmodified lines
53The wrapped function is created internally by the decorator, any positional54arguments are forwarded to the filter function. The first argument is always55the value already converted into a string.5657If you're using Jinja with django and want to use the django filters in Jinja58have a look at the `developer recipies`_ page.5960.. _designer documentation: builtins.txt61.. _developer recipies: devrecipies.txt