Ticket #256: django_ifequal_templatetag.diff
File django_ifequal_templatetag.diff, 2.6 KB (added by , 19 years ago) |
---|
-
django_src/django/core/defaulttags.py
125 125 return content 126 126 else: 127 127 return '' 128 129 class If NotEqualNode(template.Node):130 def __init__(self, var1, var2, nodelist ):128 129 class IfEqualNode(template.Node): 130 def __init__(self, var1, var2, nodelist, negate=False): 131 131 self.var1, self.var2, self.nodelist = var1, var2, nodelist 132 self.negate = negate 132 133 133 134 def __repr__(self): 134 return "<If NotEqualNode>"135 return "<IfEqualNode>" 135 136 136 137 def render(self, context): 137 if template.resolve_variable(self.var1, context) != template.resolve_variable(self.var2, context):138 if self.negate and template.resolve_variable(self.var1, context) != template.resolve_variable(self.var2, context): 138 139 return self.nodelist.render(context) 140 elif not self.negate and template.resolve_variable(self.var1, context) == template.resolve_variable(self.var2, context): 141 return self.nodelist.render(context) 139 142 else: 140 143 return '' 141 144 … … 447 450 parser.delete_first_token() 448 451 return ForNode(loopvar, sequence, reversed, nodelist_loop) 449 452 453 def do_ifequal(parser, token): 454 """ 455 Output the contents of the block if the two arguments equal each other. 456 457 Example:: 458 459 {% ifequal user.id comment.user_id %} 460 ... 461 {% endifequal %} 462 """ 463 bits = token.contents.split() 464 if len(bits) != 3: 465 raise template.TemplateSyntaxError, "'ifequal' takes two arguments" 466 nodelist = parser.parse(('endifequal',)) 467 parser.delete_first_token() 468 return IfEqualNode(bits[1], bits[2], nodelist, negate=False) 469 450 470 def do_ifnotequal(parser, token): 451 471 """ 452 472 Output the contents of the block if the two arguments do not equal each other. … … 462 482 raise template.TemplateSyntaxError, "'ifnotequal' takes two arguments" 463 483 nodelist = parser.parse(('endifnotequal',)) 464 484 parser.delete_first_token() 465 return If NotEqualNode(bits[1], bits[2], nodelist)485 return IfEqualNode(bits[1], bits[2], nodelist, negate=True) 466 486 467 487 def do_if(parser, token): 468 488 """ … … 732 752 template.register_tag('filter', do_filter) 733 753 template.register_tag('firstof', do_firstof) 734 754 template.register_tag('for', do_for) 755 template.register_tag('ifequal', do_ifequal) 735 756 template.register_tag('ifnotequal', do_ifnotequal) 736 757 template.register_tag('if', do_if) 737 758 template.register_tag('ifchanged', do_ifchanged)