Ticket #13956: indefinite_inclusion_tag_args.diff

File indefinite_inclusion_tag_args.diff, 3.3 KB (added by Stephen Burrows, 14 years ago)

Patch

  • django/template/__init__.py

     
    848848            return ''
    849849        return _render_value_in_context(output, context)
    850850
    851 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     851def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None):
    852852    "Returns a template.Node subclass."
    853853    bits = token.split_contents()[1:]
    854854    bmax = len(params)
    855855    def_len = defaults and len(defaults) or 0
    856856    bmin = bmax - def_len
    857     if(len(bits) < bmin or len(bits) > bmax):
    858         if bmin == bmax:
    859             message = "%s takes %s arguments" % (name, bmin)
    860         else:
    861             message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    862         raise TemplateSyntaxError(message)
     857    if varargs is None:
     858        if(len(bits) < bmin or len(bits) > bmax):
     859            if bmin == bmax:
     860                message = "%s takes %s arguments" % (name, bmin)
     861            else:
     862                message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
     863            raise TemplateSyntaxError(message)
     864    else:
     865        if(len(bits) < bmin):
     866            message = "%s takes at least %s arguments" % (name, bmin)
     867            raise TemplateSyntaxError(message)
    863868    return node_class(bits)
    864869
    865870class Library(object):
     
    916921        return func
    917922
    918923    def simple_tag(self,func):
    919         params, xx, xxx, defaults = getargspec(func)
     924        params, varargs, xx, defaults = getargspec(func)
    920925
    921926        class SimpleNode(Node):
    922927            def __init__(self, vars_to_resolve):
     
    926931                resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    927932                return func(*resolved_vars)
    928933
    929         compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     934        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs)
    930935        compile_func.__doc__ = func.__doc__
    931936        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    932937        return func
    933938
    934939    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
    935940        def dec(func):
    936             params, xx, xxx, defaults = getargspec(func)
     941            params, varargs, xx, defaults = getargspec(func)
    937942            if takes_context:
    938943                if params[0] == 'context':
    939944                    params = params[1:]
     
    968973                    if csrf_token is not None:
    969974                        new_context['csrf_token'] = csrf_token
    970975                    return self.nodelist.render(new_context)
    971 
    972             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
     976            compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs)
    973977            compile_func.__doc__ = func.__doc__
    974978            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    975979            return func
Back to Top