3

I need to call a filter with more than one argument.

If a filter takes only one parameter, for example "cut", we can call it with

{{ somevariable|cut:"0" }}

But if I create a custom filter which takes two parameters, I cannot call it with correct syntax.

For answers, I ask this only:

  • I don't want to send one argument and parse it in my custom filter.
  • I also don't want to chain the filters.

I think calling with two arguments is legal because there is a default filter named urlizentrunc.

def urlizetrunc(value, limit, autoescape=None):
Spycho
  • 7,698
  • 3
  • 34
  • 55
Baskaya
  • 7,651
  • 6
  • 29
  • 27
  • 1
    possible duplicate of [this question](http://stackoverflow.com/questions/420703/how-do-i-add-multiple-arguments-to-my-custom-template-filter-in-a-django-template)? – Spycho Aug 19 '11 at 10:29
  • @Spycho I don't think these are identical. I jump your url and I didn't learn how can I call urlizentrunc with legal way (not hacky as Van Gale answer) for example. – Baskaya Aug 19 '11 at 10:43
  • See the accepted answer. It says that, for custom filters, [the docs state that it cannot be done](https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters). – Spycho Aug 19 '11 at 10:58
  • @Spycho. Yes, I understand that. But it is related to custom filters. I said "How can I call a filter with more than one argument?". The filter which I mentioned is not a custom filter only and I explain it with a default filter named urlizentrunk. How can I call it with two arguments? I cannot see the answer. – Baskaya Aug 19 '11 at 23:11
  • Presumably, because it is not a custom filter, it does not need to adhere to the rules that constrain custom filters? – Spycho Aug 22 '11 at 08:53
  • Maybe you should tell anybody who can decide that the question is a duplication or not. I do not think this is a duplication of another question and I am curious and I can ask what I want to know if the question is legal. Ok? – Baskaya Aug 23 '11 at 11:28
  • I think you have misunderstood my previous comment. It has nothing to do with whether this question is a duplicate or not... – Spycho Aug 23 '11 at 12:33

2 Answers2

3

You cannot. The only work-arounds are to pass in one parameter and parse it into parts, or to have a variable external to the filter passed in.

The docs state that it cannot be done with custom filters. See this question for a more detailed explanation.

You cannot directly pass multiple parameters to non-custom filters, such as urlizetrunc either. urlizetrunc takes one parameter from the template. autoescape is set in by calling the autoescape tag with a parameter of "off" or "on". When you call urlizetrunc from the template, it passes in whatever value autoescape has been set to. You cannot pass in the value of autoescape directly from the template. See this question for a more detailed explanation.

Community
  • 1
  • 1
Spycho
  • 7,698
  • 3
  • 34
  • 55
1

You'll have to settle for taking one argument and then parsing it. The autoescape parameter is kind of special because it's there in cases your filter needs to know whether autoescaping is on or off. For more info, check out this link: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#filters-and-auto-escaping

But parsing the argument in your custom filter isn't that hard, usually it's just doing argument.split(" ") or argument.split(",")

Drekembe
  • 2,620
  • 2
  • 16
  • 13