Hi all,
I have just added a patch that allows a simple_tag to receive the
context as first argument, like inclusion_tag already does. It is
backward compatible.
I'd like to get some feedback on the code, so feel free to check it
out [1].
That issue has been raised at several occasions both on the users and
developers lists. There are some nice workaround like [2], and I
recall Malcolm agreed that it should be in django core one day [3].
The change was not trivial as simple_tag currently only takes one
argument (the decorated function) while inclusion_tag has its own set
of arguments like 'takes_context'. I got around that issue in the
patch while remaining backward compatible. The management of context
is inspired from the one in inclusion_tag.
You can still do:
@register.simple_tag
def my_tag(bla):
You can now also do:
@register.simple_tag(takes_context=True)
def my_tag(context, bla):
Note that the following (with brackets but without arguments) will be
accepted and work the same as the current code (without brackets):
@register.simple_tag()
def my_tag(bla):
Let me know what you think, and if you find it useful I could then add
a note in the doc as well.
Thanks!
Julien
[1]
http://code.djangoproject.com/ticket/7462
[2]
http://www.chipx86.com/blog/?p=245
[3]
http://groups.google.com/group/django-users/browse_thread/thread/b3b3d7894f48b609/cf74b962aa68085e