I consider this a wart and it's been bugging me for a while. So, sparked by some discussion in #django, here goes...
I'd like it if template filters supported multiple arguments for real. Currently filters that need multiple arguments just have to come up with a delimiter and then do string parsing to get the arguments. This is not a very good approach and if django.template could recognize multiple arguments it would relieve filters of this annoying duty. I'll use this syntax in my examples because it looks like what django.template already uses for multi-argument tags: {{ string|filter:"arg1" "arg2" "arg3" }} ...but if that's too much to ask, other delimiters are also reasonable, ":" and "," look okay. Here are some arguments for the feature... - Filters that need multiple *arbitrary* arguments currently aren't possible. Consider a replace filter: {{ string|replace:"this,with this" }}. This filter wouldn't be able to deal with strings containing commas. If commas are expected, the filter author would have to use some secret crazy string and hope no one uses it. Not Pythonic. - Mixing literals and template variables in arguments currently requires string concatenation in the view. Consider the slice filter: {{ articles|slice:"2:5" }}. What if I want the end index to be passed as a template variable? Currently I'd have to build a string in my view ("2:" + str(articles_end)) and pass *that* in as a template variable, which sucks. {{ articles|slice:"2" articles_end }} would be much cleaner and have the same semantics as Python's slice built-in. In one of my projects I really do need to build that silly slice string because of this limitation. Current alternatives... - Tags. Right now if you want multiple arguments you need to use tags. Is there anything inherent about tags that makes them more appropriate? I don't think so. The only reason is "because filters can only take one argument." So people are just going to make tags for their operations that should be filters, only then they can't be chained like filters can. Reasons for the current limitation... - Syntax. One goal is to keep the syntax as simple as possible. I don't really think this is an argument since tags take multiple arguments and filters could use the same syntax for argument passing. - Separation of logic. People say if the filter logic is that complex, it should be in the view. I don't think this is an argument either. How is the string replacement example or the slicing example view logic? It's still display logic. There's nothing inherently not- display-logic about a filter that takes two argument vs. a filter that just allows one. The pluralize and slice filters already prove that multiple arguments are useful; the built-in limitation is just unnecessary. Also consider this: if multiple filter arguments were currently supported, would there be a compelling reason to remove them? People would jump ship if that happened... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---