How to make django custom template tag with variable length arg list?

Custom templatetags: from django. Template import Library, Node, TemplateSyntaxError from django.utils. Encoding import smart_unicode register = Library() class FirstNotNoneNode(Node): def __init__(self, vars): self.

Vars = vars def render(self, context): for var in self. Vars: value = var. Resolve(context, True) if value is not None: return smart_unicode(value) return u'' def firstnotnone(parser,token): """ Outputs the first variable passed that is not None """ bits = token.

Split_contents()1: if len(bits).

1 Thanks, the code looks clean. Too bad it can't be done with a variable-length args list. – Jack Ha Aug 11 '10 at 12:37 You sure it can't?

– MattH Aug 11 '10 at 12:42 @Jack Ha: It handles variable length args lists, I've even tested it. As far as I can see, it's an implementation of exactly what you were asking for. – MattH Aug 11 '10 at 13:22.

The firstof tag isn't implemented via the simple_tag decorator - it uses the long form of a template. Node subclass and a separate tag function. You can see the code in django.template.

Defaulttags - it should be fairly simple to change for your purposes.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions