Hello, I'd like to bring up an itch that I've been desperate to scratch since my very early days with Django: the inability to access the context from a simple_tag.
There are especially two use cases which I encounter on a near weekly- basis: (a) Generate a simple string or some very concise html code based on the context. (b) Update the context itself. Both use cases can currently be achieved either by creating a Node class and a parser (tedious and boilerplatey) or by using an inclusion_tag (overkill having to create a template). You may also use some workarounds such as in ReviewBoard's Djblets [1], but that means you'd have to introduce a dependency in your projects or copy/paste those workarounds in every project. Really, I've always thought that life as a Django developer would be so much easier if only simple_tag could access the context... To give a bit of history: when I first got interested in this problem, I created #7462, and then was advised to close it in favour of an older one #1105. Finally, nearly two years later, #1105 got closed as a duplicate of #14262. #14262 particularly addresses use case (b), that is, create a special template tag that would make updating the context easy while presumably returning an empty string. In #1105, however, I had written a patch which would have killed two birds with one stone by addressing both (a) and (b). Plus that patch would have allowed you to update the context AND to return some result if you wanted to (instead of just returning an empty string). That patch enabled context access in simple_tag, following the same declarative syntax as for inclusion_tag: @register.simple_tag(takes_context=True) def mytag(context, foo, bar): ... It never made sense to me why inclusion_tag could access the context, and not simple_tag. One common argument I've heard is that it is because simple_tag should remain "simple". However, I don't see how enabling context access would necessarily make simple_tag "complex". >From my own developer's perspective, the "simple" in "simple_tag" mostly means that the *template syntax* should be straightforward as opposed to a complex syntax like {% mytag arg1 with arg2 as var1 and var2 %} for which you should definitely prepare yourself to go down the dark alley of Nodes and parsers (or give it a chance with the rather neat django-template-sugar [2]). I think the main reason why #1105 has been closed is that it has drifted a bit too far off. I started by adding content access in my initial patch, and then I got carried away following people's suggestions by adding access to the inner block as well. This led to the 'takes_context_plus_sink' problem that Malcolm rightly pointed out in a recent tweet [3]. Even if I'd love to have easy access to the inner block, I now admit it might be a bit much to make that work with simple_tag (probably should be a separate new ticket?). This topic has received some support from Simon [4] [5] and Jacob [6] when I initially brought it up on this list 2 years ago. I believe Malcolm and Chris Beaven, amongst others, have also expressed some interest by participating in the relevant tickets. I hope that people still want this and that there's a chance to make it ship in 1.3 (since 1.3's focus is on scratching all those annoying little itches). In summary, what do you think about enabling context access in simple_tag (using the takes_context syntax)? If the response is positive, I'm very keen to lead the way and promptly write a patch for it. A side question is: would this make #14262 redundant? Thanks! Julien :) [1] http://www.chipx86.com/blog/2008/02/29/django-development-with-djblets-custom-tag-helpers/ [2] https://github.com/alex/django-templatetag-sugar [3] http://twitter.com/malcolmt/status/13145334247063552 (click on the 'In reply to...' links to get the whole discussion) [4] http://groups.google.com/group/django-developers/browse_thread/thread/fba22c3e3c910bb9/b39a0a79ed991ca8 [5] http://groups.google.com/group/django-developers/browse_thread/thread/62d0cefde54a50a3/e58e2202ef125976 [6] http://groups.google.com/group/django-developers/browse_thread/thread/afb7c3cd93e7a659/637973df3b839b45 Relevant tickets: http://code.djangoproject.com/ticket/1105 http://code.djangoproject.com/ticket/2619 http://code.djangoproject.com/ticket/7462 http://code.djangoproject.com/ticket/14262 Also discussed in: http://groups.google.com/group/django-developers/browse_thread/thread/8737db04db7e60af/e3276998ab34f022 http://groups.google.com/group/django-developers/browse_thread/thread/d83241466444a02/bcb78463537c82f3 -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.