Ticket #7251: django-if-tag.patch
File django-if-tag.patch, 3.3 KB (added by , 16 years ago) |
---|
-
template/defaulttags.py
206 206 except VariableDoesNotExist: 207 207 val2 = None 208 208 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 209 211 return self.nodelist_true.render(context) 212 if isinstance(self.nodelist_false, Exception): 213 raise self.nodelist_false 210 214 return self.nodelist_false.render(context) 211 215 212 216 class IfNode(Node): … … 240 244 except VariableDoesNotExist: 241 245 value = None 242 246 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 243 251 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 244 256 return self.nodelist_false.render(context) 245 257 else: 246 258 for ifnot, bool_expr in self.bool_exprs: … … 249 261 except VariableDoesNotExist: 250 262 value = None 251 263 if not ((value and not ifnot) or (ifnot and not value)): 264 if isinstance(self.nodelist_false, Exception): 265 raise self.nodelist_false 252 266 return self.nodelist_false.render(context) 267 if isinstance(self.nodelist_true, Exception): 268 raise self.nodelist_true 253 269 return self.nodelist_true.render(context) 254 270 255 271 class LinkTypes: … … 640 656 if len(bits) != 3: 641 657 raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 642 658 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 644 663 token = parser.next_token() 645 664 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 647 669 parser.delete_first_token() 648 670 else: 649 671 nodelist_false = NodeList() … … 761 783 boolvars.append((True, parser.compile_filter(boolvar))) 762 784 else: 763 785 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 765 790 token = parser.next_token() 766 791 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 768 796 parser.delete_first_token() 769 797 else: 770 798 nodelist_false = NodeList()