I usually use James Bennett's django-template-utils for this purpose. It has a nice, simple implementation:
http://bitbucket.org/ubernostrum/django-template-utils/src/tip/template_utils/nodes.py#cl-11 It still requires the annoying Node/function split though. I know there have been a multitude of third party attempts at improving Django's template tags, and I think it's commonly agreed that they are one of the more boilerplatey bits. Sadly none of the third party libraries have really become defacto, so it's hard to think about including any of them in core. It does seem like a place that could use some expose either in the docs or in the general community though. Cheers, Eric On Thu, Jul 22, 2010 at 9:26 AM, Alex Robbins <alexander.j.robb...@gmail.com > wrote: > I am a huge fan of simple_tag and inclusion_tag. They take a common > template tag use case and make it very easy to write. It seems like a > common use case that isn't represented is adding a value to context. I > find myself writing tags to add a variable to context very often and > it seems like we should be able to abstract this out. > > I'm thinking it would work like this, but would love to get feedback > > @register.add_to_context_tag > def tag_name(): > do_some_python() > return context_value > > This would turn into a tag that worked like this: > > {% tag_name as variable %} > > Which puts context_value into the context as variable. > ------------------------ > > It could optionally take takes_context, and work like this. This would > make context the required first argument to the function, like > inclusion_tag. > > @register.add_to_context_tag(takes_context=True) > def tag_name(context): > do_some_python_that_needs_context(context) > return context_value > ---------------------------------- > > Finally, it could take arguments like simple_tag or inclusion tag. > > @register.add_to_context_tag > def tag_name(some, arguments, it, takes): > some_func(some, arguments) > return context_value > > which would yield a tag that worked like this: > > {% tag_name some arguments it takes as variable %} > > ----------------------------- > Is this a use case other people have? Can anyone think of a better > name than add_to_context_tag? > > Thanks, > Alex > > -- > 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<django-developers%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > > -- Eric Holscher Web Developer at The World Company in Lawrence, Ks http://ericholscher.com -- 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.