Ticket #1105: simple_tag_r1785.diff

File simple_tag_r1785.diff, 3.1 KB (added by django@…, 10 years ago)

simple_tag enhancement

  • django/core/template/__init__.py

     
    775775            raise
    776776        return self.encode_output(output)
    777777
    778 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     778def generic_tag_compiler(params, defaults, name, node_class, parser, token, takes_context=False, takes_block=False):
    779779    "Returns a template.Node subclass."
    780780    bits = token.contents.split()[1:]
    781781    bmax = len(params)
     
    787787        else:
    788788            message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    789789        raise TemplateSyntaxError, message
     790    if takes_context:
     791        node_class = curry(node_class, takes_context=takes_context)
     792    if takes_block:
     793        nodelist = parser.parse(('end' + name,))
     794        parser.delete_first_token()
     795        node_class = curry(node_class, block_nodelist=nodelist)
    790796    return node_class(bits)
    791797
    792798class Library(object):
     
    842848        self.filters[func.__name__] = func
    843849        return func
    844850
    845     def simple_tag(self,func):
    846         (params, xx, xxx, defaults) = getargspec(func)
     851    def simple_tag(self, compile_function=None, takes_block=False, takes_context=False):
     852        if compile_function == None:
     853            return curry(self.simple_tag_function, takes_block=takes_block, takes_context=takes_context)
     854        elif callable(compile_function):
     855            return self.simple_tag_function(compile_function, takes_block=takes_block, takes_context=takes_context)
     856        else:
     857            raise InvalidTemplateLibrary, "Unsupported argument to Library.simple_tag: (%r)", (compile_function,)
    847858
     859    def simple_tag_function(self, func, takes_block=False, takes_context=False):
    848860        class SimpleNode(Node):
    849             def __init__(self, vars_to_resolve):
     861            def __init__(self, vars_to_resolve, takes_context=False, block_nodelist=None):
    850862                self.vars_to_resolve = vars_to_resolve
     863                self.takes_context, self.block_nodelist = takes_context, block_nodelist
    851864
    852865            def render(self, context):
    853866                resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
    854                 return func(*resolved_vars)
     867                if self.block_nodelist:
     868                    resolved_vars.insert(0, self.block_nodelist)
     869                if self.takes_context:
     870                    resolved_vars.insert(0, context)
     871                rendered = func(*resolved_vars)
     872                return rendered or ''
    855873
    856         compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, SimpleNode)
     874        (params, xx, xxx, defaults) = getargspec(func)
     875        taken_args = sum([takes_block, takes_context])
     876        compile_func = curry(generic_tag_compiler, params[taken_args:], defaults, func.__name__, SimpleNode, takes_block=takes_block, takes_context=takes_context)
    857877        compile_func.__doc__ = func.__doc__
    858878        self.tag(func.__name__, compile_func)
    859879        return func
Back to Top