开发者

how to make django custom template tag with variable length arg list

开发者 https://www.devze.com 2023-01-10 18:50 出处:网络
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, firstnotnon

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


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消