Ticket #1105: simple_tag_r1785.diff
File simple_tag_r1785.diff, 3.1 KB (added by , 19 years ago) |
---|
-
django/core/template/__init__.py
775 775 raise 776 776 return self.encode_output(output) 777 777 778 def generic_tag_compiler(params, defaults, name, node_class, parser, token ):778 def generic_tag_compiler(params, defaults, name, node_class, parser, token, takes_context=False, takes_block=False): 779 779 "Returns a template.Node subclass." 780 780 bits = token.contents.split()[1:] 781 781 bmax = len(params) … … 787 787 else: 788 788 message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 789 789 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) 790 796 return node_class(bits) 791 797 792 798 class Library(object): … … 842 848 self.filters[func.__name__] = func 843 849 return func 844 850 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,) 847 858 859 def simple_tag_function(self, func, takes_block=False, takes_context=False): 848 860 class SimpleNode(Node): 849 def __init__(self, vars_to_resolve ):861 def __init__(self, vars_to_resolve, takes_context=False, block_nodelist=None): 850 862 self.vars_to_resolve = vars_to_resolve 863 self.takes_context, self.block_nodelist = takes_context, block_nodelist 851 864 852 865 def render(self, context): 853 866 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 '' 855 873 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) 857 877 compile_func.__doc__ = func.__doc__ 858 878 self.tag(func.__name__, compile_func) 859 879 return func