Django

Code

Ticket #7251: django-if-tag.patch

File django-if-tag.patch, 3.3 kB (added by Joshua 'jag' Ginsberg <jag@fsf.org>, 4 months ago)
  • template/defaulttags.py

    old new  
    206206        except VariableDoesNotExist: 
    207207            val2 = None 
    208208        if (self.negate and val1 != val2) or (not self.negate and val1 == val2): 
     209            if isinstance(self.nodelist_true, Exception): 
     210                raise self.nodelist_true 
    209211            return self.nodelist_true.render(context) 
     212        if isinstance(self.nodelist_false, Exception): 
     213            raise self.nodelist_false 
    210214        return self.nodelist_false.render(context) 
    211215 
    212216class IfNode(Node): 
     
    240244                except VariableDoesNotExist: 
    241245                    value = None 
    242246                if (value and not ifnot) or (ifnot and not value): 
     247                    # if the nodelist_true branch evaluated to an error 
     248                    # raise it now 
     249                    if isinstance(self.nodelist_true, Exception): 
     250                        raise self.nodelist_true 
    243251                    return self.nodelist_true.render(context) 
     252            # if the nodelist_false brance evaluated to an error 
     253            # raise it now 
     254            if isinstance(self.nodelist_false, Exception): 
     255                raise self.nodelist_false 
    244256            return self.nodelist_false.render(context) 
    245257        else: 
    246258            for ifnot, bool_expr in self.bool_exprs: 
     
    249261                except VariableDoesNotExist: 
    250262                    value = None 
    251263                if not ((value and not ifnot) or (ifnot and not value)): 
     264                    if isinstance(self.nodelist_false, Exception): 
     265                        raise self.nodelist_false 
    252266                    return self.nodelist_false.render(context) 
     267            if isinstance(self.nodelist_true, Exception): 
     268                raise self.nodelist_true 
    253269            return self.nodelist_true.render(context) 
    254270 
    255271    class LinkTypes: 
     
    640656    if len(bits) != 3: 
    641657        raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 
    642658    end_tag = 'end' + bits[0] 
    643     nodelist_true = parser.parse(('else', end_tag)) 
     659    try: 
     660        nodelist_true = parser.parse(('else', end_tag)) 
     661    except Exception, e: 
     662        nodelist_true = e 
    644663    token = parser.next_token() 
    645664    if token.contents == 'else': 
    646         nodelist_false = parser.parse((end_tag,)) 
     665        try: 
     666            nodelist_false = parser.parse((end_tag,)) 
     667        except Exception, e: 
     668            nodelist_false = e 
    647669        parser.delete_first_token() 
    648670    else: 
    649671        nodelist_false = NodeList() 
     
    761783            boolvars.append((True, parser.compile_filter(boolvar))) 
    762784        else: 
    763785            boolvars.append((False, parser.compile_filter(boolpair))) 
    764     nodelist_true = parser.parse(('else', 'endif')) 
     786    try: 
     787        nodelist_true = parser.parse(('else', 'endif')) 
     788    except Exception, e: 
     789        nodelist_true = e 
    765790    token = parser.next_token() 
    766791    if token.contents == 'else': 
    767         nodelist_false = parser.parse(('endif',)) 
     792        try: 
     793            nodelist_false = parser.parse(('endif',)) 
     794        except Exception, e: 
     795            nodelist_false = e 
    768796        parser.delete_first_token() 
    769797    else: 
    770798        nodelist_false = NodeList()