Ticket #6766: template_if_invalid.diff

File template_if_invalid.diff, 7.0 KB (added by Thomas Güttler, 17 years ago)

Previous patch included the function twice.

  • tests/regressiontests/templates/tests.py

     
    568568            'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"),
    569569            'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"),
    570570            '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')),
    573573
    574574            # 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')),
    576576            'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"),
    577577            'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"),
    578578            'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"),
     
    590590            'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'),
    591591            'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'),
    592592            '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.')),
    594594            'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''),
    595595            'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'),
    596596            'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'),
     
    759759
    760760            'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),
    761761            'invalidstr02': ('{{ var|default_if_none:"Foo" }}', {}, ('','INVALID')),
    762             'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),
     762            'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ('', 'INVALID')),
    763763            'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'),
    764764            'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'),
    765765            'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
    766766            'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
     767            'invalidstr07': ('{% ifequal var "abc" %}{{ var }}{% endifequal %}', {}, ('', 'INVALID %s', 'var')),
    767768
    768769            ### MULTILINE #############################################################
    769770
  • django/template/__init__.py

     
    9999# True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means
    100100# uninitialised.
    101101invalid_var_format_string = None
     102def 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
    102109
    103110class TemplateSyntaxError(Exception):
    104111    def __str__(self):
     
    510517        self.filters = filters
    511518        self.var = Variable(var)
    512519
    513     def resolve(self, context, ignore_failures=False):
     520    def resolve(self, context, ignore_failures=False, raise_failures=False):
    514521        try:
    515522            obj = self.var.resolve(context)
    516523        except VariableDoesNotExist:
    517524            if ignore_failures:
    518525                obj = None
     526            elif raise_failures:
     527                raise
    519528            else:
    520529                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)
    527531                else:
    528532                    obj = settings.TEMPLATE_STRING_IF_INVALID
    529533        for func, args in self.filters:
  • django/template/defaulttags.py

     
    88except NameError:
    99    from django.utils.itercompat import reversed     # Python 2.3 fallback
    1010
    11 from django.template import Node, NodeList, Template, Context, Variable
     11from django.template import Node, NodeList, Template, Context, Variable, template_string_if_invalid
    1212from 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
    1313from django.template import get_library, Library, InvalidTemplateLibrary
    1414from django.conf import settings
     
    114114            parentloop = {}
    115115        context.push()
    116116        try:
    117             values = self.sequence.resolve(context, True)
     117            values = self.sequence.resolve(context, raise_failures=True)
    118118        except VariableDoesNotExist:
     119            if settings.TEMPLATE_STRING_IF_INVALID:
     120                return settings.TEMPLATE_STRING_IF_INVALID % self.sequence
    119121            values = []
    120122        if values is None:
    121123            values = []
     
    200202        try:
    201203            val1 = self.var1.resolve(context)
    202204        except VariableDoesNotExist:
     205            if settings.TEMPLATE_STRING_IF_INVALID:
     206                return template_string_if_invalid(self.var1)
    203207            val1 = None
    204208        try:
    205209            val2 = self.var2.resolve(context)
    206210        except VariableDoesNotExist:
     211            if settings.TEMPLATE_STRING_IF_INVALID:
     212                return template_string_if_invalid(self.var2)
    207213            val2 = None
    208214        if (self.negate and val1 != val2) or (not self.negate and val1 == val2):
    209215            return self.nodelist_true.render(context)
Back to Top