Proposal: Enhanced simple-tag decorator #1105

8 views
Skip to first unread message

Julien Phalip

unread,
Sep 16, 2008, 8:03:49 AM9/16/08
to Django developers
This is a call to review ticket #1105. The proposed patch is fully
functional, backwards-compatible and contains tests and documentation.
http://code.djangoproject.com/ticket/1105

Currently it is not possible to access the context from within a
simple_tag, and there is no "simple" way to create block tags. Both of
these tasks imply using some boilerplate code to parse the tokens,
manage variables, etc.

The patch in #1105 addresses these two issues. What you can now do:

1) Get access to the context:

@register.simple_tag(takes_context=True)
def mytag(context, arg1, arg2):
...

2) Easily create simple block tags:

@register.simple_tag(takes_nodelist=True)
def mytag(nodelist, arg1, arg2):
...

{% mytag some_var "some_string" %}
...
{% endmytag %}

3) Or even do both:

@register.simple_tag(takes_context=True, takes_nodelist=True)
def mytag(context, nodelist, arg1, arg2):
...

These enhancements should make the development of simple template tags
much faster. The patch's tests also make sure that it remains
backwards-compatible and won't break existing code. I've added this to
the Version 1.1 feature wish-list.

Any feedback welcome, thanks!

Julien :)

Simon Willison

unread,
Sep 16, 2008, 11:55:00 AM9/16/08
to Django developers
On Sep 16, 1:03 pm, Julien Phalip <jpha...@gmail.com> wrote:
> This is a call to review ticket #1105. The proposed patch is fully
> functional, backwards-compatible and contains tests and documentation.http://code.djangoproject.com/ticket/1105
>
> Currently it is not possible to access the context from within a
> simple_tag, and there is no "simple" way to create block tags. Both of
> these tasks imply using some boilerplate code to parse the tokens,
> manage variables, etc.

+1, I love it. I've been using this in my own projects for ages.

Cheers,

Simon
Reply all
Reply to author
Forward
0 new messages