views:

52

answers:

2

I'm writing a custom template tag 'firstnotnone', similar to the 'firstof' template tag of Django. How to use variable length arguments? The code below results in TemplateSyntaxError, firstnotnone takes 1 arguments.

Template:

{% load library %}
{% firstnotnone 'a' 'b' 'c' %}

Custom template tag library:

@register.simple_tag
def firstnotnone(*args):
    print args
    for arg in args:
        if arg is not None:
            return arg
+3  A: 

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.

Daniel Roseman
+1  A: 

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:
        raise TemplateSyntaxError("'firstnotnone' statement requires at least one argument")
    return FirstNotNoneNode([parser.compile_filter(bit) for bit in bits])

firstnotnone = register.tag(firstnotnone)
MattH
Thanks, the code looks clean. Too bad it can't be done with a variable-length args list.
Jack Ha
You sure it can't?
MattH
@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