Ticket #256: django_ifequal_templatetag.diff

File django_ifequal_templatetag.diff, 2.6 KB (added by alastair@…, 19 years ago)
  • django_src/django/core/defaulttags.py

     
    125125            return content
    126126        else:
    127127            return ''
    128 
    129 class IfNotEqualNode(template.Node):
    130     def __init__(self, var1, var2, nodelist):
     128           
     129class IfEqualNode(template.Node):
     130    def __init__(self, var1, var2, nodelist, negate=False):
    131131        self.var1, self.var2, self.nodelist = var1, var2, nodelist
     132        self.negate = negate
    132133
    133134    def __repr__(self):
    134         return "<IfNotEqualNode>"
     135        return "<IfEqualNode>"
    135136
    136137    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):
    138139            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)
    139142        else:
    140143            return ''
    141144
     
    447450    parser.delete_first_token()
    448451    return ForNode(loopvar, sequence, reversed, nodelist_loop)
    449452
     453def 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
    450470def do_ifnotequal(parser, token):
    451471    """
    452472    Output the contents of the block if the two arguments do not equal each other.
     
    462482        raise template.TemplateSyntaxError, "'ifnotequal' takes two arguments"
    463483    nodelist = parser.parse(('endifnotequal',))
    464484    parser.delete_first_token()
    465     return IfNotEqualNode(bits[1], bits[2], nodelist)
     485    return IfEqualNode(bits[1], bits[2], nodelist, negate=True)
    466486
    467487def do_if(parser, token):
    468488    """
     
    732752template.register_tag('filter', do_filter)
    733753template.register_tag('firstof', do_firstof)
    734754template.register_tag('for', do_for)
     755template.register_tag('ifequal', do_ifequal)
    735756template.register_tag('ifnotequal', do_ifnotequal)
    736757template.register_tag('if', do_if)
    737758template.register_tag('ifchanged', do_ifchanged)
Back to Top