Context support for simple_tag #7462

0 views
Skip to first unread message

Julien Phalip

unread,
Jun 16, 2008, 5:00:21 AM6/16/08
to Django developers
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

Simon Willison

unread,
Jun 16, 2008, 6:16:43 AM6/16/08
to Django developers
On Jun 16, 10:00 am, Julien Phalip <jpha...@gmail.com> wrote:
> 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].

Looks great to me. With the addition of some regression tests to prove
that it works and doesn't break the old behaviour I'd love to see this
checked in.

Cheers,

Simon

Julien Phalip

unread,
Jun 16, 2008, 6:40:24 AM6/16/08
to Django developers
Hi Simon,

I'm a bit unsure where to stick the regression test for this. Could
you give any pointer?

Thanks!

Julien

Russell Keith-Magee

unread,
Jun 16, 2008, 7:08:32 AM6/16/08
to django-d...@googlegroups.com
On Mon, Jun 16, 2008 at 6:40 PM, Julien Phalip <jph...@gmail.com> wrote:
>
> Hi Simon,
>
> I'm a bit unsure where to stick the regression test for this. Could
> you give any pointer?

The templates regression tests would be a logical place;
tests/regressiontests/templates. Have a poke around to see if there is
anywhere appropriate in the existing tests; if not, add a completely
new test module.

Yours,
Russ Magee %-)

Julien Phalip

unread,
Jun 16, 2008, 10:14:41 AM6/16/08
to Django developers
Hi,

I updated the patch with regression tests and documentation. Any
feedback welcome!

Julien

On Jun 16, 9:08 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages