Ticket #6766: template_if_invalid.diff
File template_if_invalid.diff, 7.0 KB (added by , 17 years ago) |
---|
-
tests/regressiontests/templates/tests.py
568 568 'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"), 569 569 'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"), 570 570 'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"), 571 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),572 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),571 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, ('no', 'INVALID %s', 'a')), 572 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, ('yes', 'INVALID %s', 'a')), 573 573 574 574 # SMART SPLITTING 575 'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, "no"),575 'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, ('no', 'INVALID %s', 'a')), 576 576 'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"), 577 577 'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"), 578 578 'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"), … … 590 590 'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'), 591 591 'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'), 592 592 'ifequal-numeric06': ('{% ifequal x .2 %}yes{% endifequal %}', {'x': .2}, 'yes'), 593 'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ''),593 'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ('', 'INVALID %s', '2.')), 594 594 'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''), 595 595 'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'), 596 596 'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'), … … 759 759 760 760 'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')), 761 761 'invalidstr02': ('{{ var|default_if_none:"Foo" }}', {}, ('','INVALID')), 762 'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),762 'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ('', 'INVALID')), 763 763 'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'), 764 764 'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'), 765 765 'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')), 766 766 'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')), 767 'invalidstr07': ('{% ifequal var "abc" %}{{ var }}{% endifequal %}', {}, ('', 'INVALID %s', 'var')), 767 768 768 769 ### MULTILINE ############################################################# 769 770 -
django/template/__init__.py
99 99 # True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means 100 100 # uninitialised. 101 101 invalid_var_format_string = None 102 def template_string_if_invalid(variable): 103 global invalid_var_format_string 104 if invalid_var_format_string is None: 105 invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID 106 if invalid_var_format_string: 107 return settings.TEMPLATE_STRING_IF_INVALID % variable 108 return settings.TEMPLATE_STRING_IF_INVALID 102 109 103 110 class TemplateSyntaxError(Exception): 104 111 def __str__(self): … … 510 517 self.filters = filters 511 518 self.var = Variable(var) 512 519 513 def resolve(self, context, ignore_failures=False ):520 def resolve(self, context, ignore_failures=False, raise_failures=False): 514 521 try: 515 522 obj = self.var.resolve(context) 516 523 except VariableDoesNotExist: 517 524 if ignore_failures: 518 525 obj = None 526 elif raise_failures: 527 raise 519 528 else: 520 529 if settings.TEMPLATE_STRING_IF_INVALID: 521 global invalid_var_format_string 522 if invalid_var_format_string is None: 523 invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID 524 if invalid_var_format_string: 525 return settings.TEMPLATE_STRING_IF_INVALID % self.var 526 return settings.TEMPLATE_STRING_IF_INVALID 530 return template_string_if_invalid(self.var) 527 531 else: 528 532 obj = settings.TEMPLATE_STRING_IF_INVALID 529 533 for func, args in self.filters: -
django/template/defaulttags.py
8 8 except NameError: 9 9 from django.utils.itercompat import reversed # Python 2.3 fallback 10 10 11 from django.template import Node, NodeList, Template, Context, Variable 11 from django.template import Node, NodeList, Template, Context, Variable, template_string_if_invalid 12 12 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END 13 13 from django.template import get_library, Library, InvalidTemplateLibrary 14 14 from django.conf import settings … … 114 114 parentloop = {} 115 115 context.push() 116 116 try: 117 values = self.sequence.resolve(context, True)117 values = self.sequence.resolve(context, raise_failures=True) 118 118 except VariableDoesNotExist: 119 if settings.TEMPLATE_STRING_IF_INVALID: 120 return settings.TEMPLATE_STRING_IF_INVALID % self.sequence 119 121 values = [] 120 122 if values is None: 121 123 values = [] … … 200 202 try: 201 203 val1 = self.var1.resolve(context) 202 204 except VariableDoesNotExist: 205 if settings.TEMPLATE_STRING_IF_INVALID: 206 return template_string_if_invalid(self.var1) 203 207 val1 = None 204 208 try: 205 209 val2 = self.var2.resolve(context) 206 210 except VariableDoesNotExist: 211 if settings.TEMPLATE_STRING_IF_INVALID: 212 return template_string_if_invalid(self.var2) 207 213 val2 = None 208 214 if (self.negate and val1 != val2) or (not self.negate and val1 == val2): 209 215 return self.nodelist_true.render(context)