Ticket #13956: indefinite_inclusion_tag_args_with_tests.diff
File indefinite_inclusion_tag_args_with_tests.diff, 7.3 KB (added by , 14 years ago) |
---|
-
django/template/__init__.py
=== modified file 'django/template/__init__.py'
848 848 return '' 849 849 return _render_value_in_context(output, context) 850 850 851 def generic_tag_compiler(params, defaults, name, node_class, parser, token ):851 def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None): 852 852 "Returns a template.Node subclass." 853 853 bits = token.split_contents()[1:] 854 854 bmax = len(params) 855 855 def_len = defaults and len(defaults) or 0 856 856 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) 863 868 return node_class(bits) 864 869 865 870 class Library(object): … … 916 921 return func 917 922 918 923 def simple_tag(self,func): 919 params, xx, xxx, defaults = getargspec(func)924 params, varargs, xx, defaults = getargspec(func) 920 925 921 926 class SimpleNode(Node): 922 927 def __init__(self, vars_to_resolve): … … 926 931 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 927 932 return func(*resolved_vars) 928 933 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) 930 935 compile_func.__doc__ = func.__doc__ 931 936 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 932 937 return func 933 938 934 939 def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 935 940 def dec(func): 936 params, xx, xxx, defaults = getargspec(func)941 params, varargs, xx, defaults = getargspec(func) 937 942 if takes_context: 938 943 if params[0] == 'context': 939 944 params = params[1:] … … 968 973 if csrf_token is not None: 969 974 new_context['csrf_token'] = csrf_token 970 975 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) 973 977 compile_func.__doc__ = func.__doc__ 974 978 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 975 979 return func -
tests/regressiontests/templates/simple_tag.py
=== added file 'tests/regressiontests/templates/simple_tag.py'
1 import unittest 2 from django.template import Context, Template, TemplateSyntaxError 3 4 5 class SimpleTagTests(unittest.TestCase): 6 def test_simple_tag_with_fixed_number_of_arguments(self): 7 c = Context({}) 8 t = Template('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}') 9 self.assertEquals(t.render(c), u'one, two, three') 10 11 c = Context({'one': '1', 'two': '2', 'three': '3'}) 12 t = Template('{% load custom %}{% simple_tag_with_three_arguments one two three %}') 13 self.assertEquals(t.render(c), u'1, 2, 3') 14 15 self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}') 16 self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}') 17 18 def test_simple_tag_with_var_number_of_arguments(self): 19 c = Context({}) 20 t = Template('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}') 21 self.assertEquals(t.render(c), u'one, two, three') 22 23 c = Context({'one': '1', 'two': '2', 'three': '3'}) 24 t = Template('{% load custom %}{% simple_tag_with_two_default_arguments one three %}') 25 self.assertEquals(t.render(c), u'1, 3, 3') 26 27 c = Context({'one': 'first'}) 28 t = Template('{% load custom %}{% simple_tag_with_two_default_arguments one %}') 29 self.assertEquals(t.render(c), u'first, 2, 3') 30 31 self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments %}') 32 self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}') 33 34 def test_simple_tag_with_unlimited_number_of_arguments(self): 35 c = Context({}) 36 t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}') 37 self.assertEquals(t.render(c), u'one, two, three') 38 39 c = Context({}) 40 t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}') 41 self.assertEquals(t.render(c), u'one, 2') 42 43 c = Context({'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'}) 44 t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments one two three four five %}') 45 self.assertEquals(t.render(c), u'1, 2, 3, 4, 5') 46 47 c = Context({'one': 'first'}) 48 t = Template('{% load custom %}{% simple_tag_with_unlimited_arguments one %}') 49 self.assertEquals(t.render(c), u'first, 2') 50 51 self.assertRaises(TemplateSyntaxError, Template, '{% load custom %}{% simple_tag_with_two_default_arguments %}') -
tests/regressiontests/templates/templatetags/custom.py
=== modified file 'tests/regressiontests/templates/templatetags/custom.py'
9 9 10 10 register.filter(trim) 11 11 12 13 def simple_tag_with_three_arguments(one, two, three): 14 return ', '.join((one, two, three)) 15 16 register.simple_tag(simple_tag_with_three_arguments) 17 18 19 def simple_tag_with_two_default_arguments(one, two='2', three='3'): 20 return ', '.join((one, two, three)) 21 22 register.simple_tag(simple_tag_with_two_default_arguments) 23 24 25 def simple_tag_with_unlimited_arguments(one, two='2', *args): 26 return ', '.join([one, two] + list(args)) 27 28 register.simple_tag(simple_tag_with_unlimited_arguments) -
tests/regressiontests/templates/tests.py
=== modified file 'tests/regressiontests/templates/tests.py'
27 27 from unicode import unicode_tests 28 28 from nodelist import NodelistTest 29 29 from smartif import * 30 from simple_tag import SimpleTagTests 30 31 31 32 try: 32 33 from loaders import *