Index: tests/regressiontests/templates/tests.py
===================================================================
--- tests/regressiontests/templates/tests.py	(Revision 7235)
+++ tests/regressiontests/templates/tests.py	(Arbeitskopie)
@@ -568,11 +568,11 @@
             'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"),
             'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"),
             'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"),
-            'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
-            'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
+            'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, ('no', 'INVALID %s', 'a')),
+            'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, ('yes', 'INVALID %s', 'a')),
 
             # SMART SPLITTING
-            'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, "no"),
+            'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, ('no', 'INVALID %s', 'a')),
             'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"),
             'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"),
             'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"),
@@ -590,7 +590,7 @@
             'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'),
             'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'),
             'ifequal-numeric06': ('{% ifequal x .2 %}yes{% endifequal %}', {'x': .2}, 'yes'),
-            'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ''),
+            'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ('', 'INVALID %s', '2.')),
             'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''),
             'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'),
             'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'),
@@ -759,11 +759,12 @@
 
             'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),
             'invalidstr02': ('{{ var|default_if_none:"Foo" }}', {}, ('','INVALID')),
-            'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ''),
+            'invalidstr03': ('{% for v in var %}({{ v }}){% endfor %}', {}, ('', 'INVALID')),
             'invalidstr04': ('{% if var %}Yes{% else %}No{% endif %}', {}, 'No'),
             'invalidstr04': ('{% if var|default:"Foo" %}Yes{% else %}No{% endif %}', {}, 'Yes'),
             'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
             'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
+            'invalidstr07': ('{% ifequal var "abc" %}{{ var }}{% endifequal %}', {}, ('', 'INVALID %s', 'var')),
 
             ### MULTILINE #############################################################
 
Index: django/template/__init__.py
===================================================================
--- django/template/__init__.py	(Revision 7235)
+++ django/template/__init__.py	(Arbeitskopie)
@@ -99,6 +99,13 @@
 # True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means
 # uninitialised.
 invalid_var_format_string = None
+def template_string_if_invalid(variable):
+    global invalid_var_format_string
+    if invalid_var_format_string is None:
+        invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID
+    if invalid_var_format_string:
+        return settings.TEMPLATE_STRING_IF_INVALID % variable
+    return settings.TEMPLATE_STRING_IF_INVALID
 
 class TemplateSyntaxError(Exception):
     def __str__(self):
@@ -510,20 +517,17 @@
         self.filters = filters
         self.var = Variable(var)
 
-    def resolve(self, context, ignore_failures=False):
+    def resolve(self, context, ignore_failures=False, raise_failures=False):
         try:
             obj = self.var.resolve(context)
         except VariableDoesNotExist:
             if ignore_failures:
                 obj = None
+            elif raise_failures:
+                raise
             else:
                 if settings.TEMPLATE_STRING_IF_INVALID:
-                    global invalid_var_format_string
-                    if invalid_var_format_string is None:
-                        invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID
-                    if invalid_var_format_string:
-                        return settings.TEMPLATE_STRING_IF_INVALID % self.var
-                    return settings.TEMPLATE_STRING_IF_INVALID
+                    return template_string_if_invalid(self.var)
                 else:
                     obj = settings.TEMPLATE_STRING_IF_INVALID
         for func, args in self.filters:
Index: django/template/defaulttags.py
===================================================================
--- django/template/defaulttags.py	(Revision 7235)
+++ django/template/defaulttags.py	(Arbeitskopie)
@@ -8,7 +8,7 @@
 except NameError:
     from django.utils.itercompat import reversed     # Python 2.3 fallback
 
-from django.template import Node, NodeList, Template, Context, Variable
+from django.template import Node, NodeList, Template, Context, Variable, template_string_if_invalid
 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
 from django.template import get_library, Library, InvalidTemplateLibrary
 from django.conf import settings
@@ -114,8 +114,10 @@
             parentloop = {}
         context.push()
         try:
-            values = self.sequence.resolve(context, True)
+            values = self.sequence.resolve(context, raise_failures=True)
         except VariableDoesNotExist:
+            if settings.TEMPLATE_STRING_IF_INVALID:
+                return settings.TEMPLATE_STRING_IF_INVALID % self.sequence
             values = []
         if values is None:
             values = []
@@ -200,10 +202,14 @@
         try:
             val1 = self.var1.resolve(context)
         except VariableDoesNotExist:
+            if settings.TEMPLATE_STRING_IF_INVALID:
+                return template_string_if_invalid(self.var1)
             val1 = None
         try:
             val2 = self.var2.resolve(context)
         except VariableDoesNotExist:
+            if settings.TEMPLATE_STRING_IF_INVALID:
+                return template_string_if_invalid(self.var2)
             val2 = None
         if (self.negate and val1 != val2) or (not self.negate and val1 == val2):
             return self.nodelist_true.render(context)
