Django

Code

Changeset 574

Show
Ignore:
Timestamp:
08/29/05 16:11:41 (3 years ago)
Author:
adrian
Message:

Fixed #256 and #334 -- Added {% ifequal %} template tag. Also, {% ifequal %} and {% ifnotequal %} now both accept an optional {% else %} clause. Unit tests included.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/defaulttags.py

    r496 r574  
    127127            return '' 
    128128 
    129 class IfNotEqualNode(template.Node): 
    130     def __init__(self, var1, var2, nodelist): 
    131         self.var1, self.var2, self.nodelist = var1, var2, nodelist 
     129class IfEqualNode(template.Node): 
     130    def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): 
     131        self.var1, self.var2 = var1, var2 
     132        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 
     133        self.negate = negate 
    132134 
    133135    def __repr__(self): 
    134         return "<IfNotEqualNode>" 
    135  
    136     def render(self, context): 
    137         if template.resolve_variable(self.var1, context) != template.resolve_variable(self.var2, context): 
    138             return self.nodelist.render(context) 
    139         else: 
    140             return '' 
     136        return "<IfEqualNode>" 
     137 
     138    def render(self, context): 
     139        val1 = template.resolve_variable(self.var1, context) 
     140        val2 = template.resolve_variable(self.var2, context) 
     141        if (self.negate and val1 != val2) or (not self.negate and val1 == val2): 
     142            return self.nodelist_true.render(context) 
     143        return self.nodelist_false.render(context) 
    141144 
    142145class IfNode(template.Node): 
     
    450453    return ForNode(loopvar, sequence, reversed, nodelist_loop) 
    451454 
    452 def do_ifnotequal(parser, token): 
    453     """ 
    454     Output the contents of the block if the two arguments do not equal each other. 
    455  
    456     Example:: 
     455def do_ifequal(parser, token, negate): 
     456    """ 
     457    Output the contents of the block if the two arguments equal/don't equal each other. 
     458 
     459    Examples:: 
     460 
     461        {% ifequal user.id comment.user_id %} 
     462            ... 
     463        {% endifequal %} 
    457464 
    458465        {% ifnotequal user.id comment.user_id %} 
    459466            ... 
     467        {% else %} 
     468            ... 
    460469        {% endifnotequal %} 
    461470    """ 
    462471    bits = token.contents.split() 
    463472    if len(bits) != 3: 
    464         raise template.TemplateSyntaxError, "'ifnotequal' takes two arguments" 
    465     nodelist = parser.parse(('endifnotequal',)) 
    466     parser.delete_first_token() 
    467     return IfNotEqualNode(bits[1], bits[2], nodelist) 
     473        raise template.TemplateSyntaxError, "%r takes two arguments" % bits[0] 
     474    end_tag = 'end' + bits[0] 
     475    nodelist_true = parser.parse(('else', end_tag)) 
     476    token = parser.next_token() 
     477    if token.contents == 'else': 
     478        nodelist_false = parser.parse((end_tag,)) 
     479        parser.delete_first_token() 
     480    else: 
     481        nodelist_false = template.NodeList() 
     482    return IfEqualNode(bits[1], bits[2], nodelist_true, nodelist_false, negate) 
    468483 
    469484def do_if(parser, token): 
     
    737752template.register_tag('firstof', do_firstof) 
    738753template.register_tag('for', do_for) 
    739 template.register_tag('ifnotequal', do_ifnotequal) 
     754template.register_tag('ifequal', lambda parser, token: do_ifequal(parser, token, False)) 
     755template.register_tag('ifnotequal', lambda parser, token: do_ifequal(parser, token, True)) 
    740756template.register_tag('if', do_if) 
    741757template.register_tag('ifchanged', do_ifchanged) 
  • django/trunk/tests/othertests/templates.py

    r367 r574  
    55    def __init__(self): 
    66        self.otherclass = OtherClass() 
    7          
     7 
    88    def method(self): 
    99        return "SomeClass.method" 
    10          
     10 
    1111    def method2(self, o): 
    1212        return o 
     
    1515    def method(self): 
    1616        return "OtherClass.method" 
    17          
     17 
    1818# SYNTAX -- 
    1919# 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class) 
     
    2121 
    2222    ### BASIC SYNTAX ########################################################## 
    23      
     23 
    2424    # Plain text should go through the template parser untouched 
    2525    'basic-syntax01': ("something cool", {}, "something cool"), 
    26      
     26 
    2727    # Variables should be replaced with their value in the current context 
    2828    'basic-syntax02': ("{{ headline }}", {'headline':'Success'}, "Success"), 
     
    3030    # More than one replacement variable is allowed in a template 
    3131    'basic-syntax03': ("{{ first }} --- {{ second }}", {"first" : 1, "second" : 2}, "1 --- 2"), 
    32      
     32 
    3333    # Fail silently when a variable is not found in the current context 
    3434    'basic-syntax04': ("as{{ missing }}df", {}, "asdf"), 
    35      
     35 
    3636    # A variable may not contain more than one word 
    3737    'basic-syntax06': ("{{ multi word variable }}", {}, template.TemplateSyntaxError), 
    38      
     38 
    3939    # Raise TemplateSyntaxError for empty variable tags 
    4040    'basic-syntax07': ("{{ }}",        {}, template.TemplateSyntaxError), 
    4141    'basic-syntax08': ("{{        }}", {}, template.TemplateSyntaxError), 
    42      
     42 
    4343    # Attribute syntax allows a template to call an object's attribute 
    4444    'basic-syntax09': ("{{ var.method }}", {"var": SomeClass()}, "SomeClass.method"), 
    45      
     45 
    4646    # Multiple levels of attribute access are allowed 
    4747    'basic-syntax10': ("{{ var.otherclass.method }}", {"var": SomeClass()}, "OtherClass.method"), 
    48      
     48 
    4949    # Fail silently when a variable's attribute isn't found 
    5050    'basic-syntax11': ("{{ var.blech }}", {"var": SomeClass()}, ""), 
    51      
     51 
    5252    # Raise TemplateSyntaxError when trying to access a variable beginning with an underscore 
    5353    'basic-syntax12': ("{{ var.__dict__ }}", {"var": SomeClass()}, template.TemplateSyntaxError), 
    54      
     54 
    5555    # Raise TemplateSyntaxError when trying to access a variable containing an illegal character 
    5656    'basic-syntax13': ("{{ va>r }}", {}, template.TemplateSyntaxError), 
     
    5959    'basic-syntax16': ("{{ eggs! }}", {}, template.TemplateSyntaxError), 
    6060    'basic-syntax17': ("{{ moo? }}", {}, template.TemplateSyntaxError), 
    61      
     61 
    6262    # Attribute syntax allows a template to call a dictionary key's value 
    6363    'basic-syntax18': ("{{ foo.bar }}", {"foo" : {"bar" : "baz"}}, "baz"), 
    64      
     64 
    6565    # Fail silently when a variable's dictionary key isn't found 
    6666    'basic-syntax19': ("{{ foo.spam }}", {"foo" : {"bar" : "baz"}}, ""), 
    67      
     67 
    6868    # Fail silently when accessing a non-simple method 
    6969    'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ""), 
    70          
     70 
    7171    # Basic filter usage 
    7272    'basic-syntax21': ("{{ var|upper }}", {"var": "Django is the greatest!"}, "DJANGO IS THE GREATEST!"), 
    73      
     73 
    7474    # Chained filters 
    7575    'basic-syntax22': ("{{ var|upper|lower }}", {"var": "Django is the greatest!"}, "django is the greatest!"), 
    76      
     76 
    7777    # Raise TemplateSyntaxError for space between a variable and filter pipe 
    7878    'basic-syntax23': ("{{ var |upper }}", {}, template.TemplateSyntaxError), 
    79      
     79 
    8080    # Raise TemplateSyntaxError for space after a filter pipe 
    8181    'basic-syntax24': ("{{ var| upper }}", {}, template.TemplateSyntaxError), 
    82      
     82 
    8383    # Raise TemplateSyntaxError for a nonexistent filter 
    8484    'basic-syntax25': ("{{ var|does_not_exist }}", {}, template.TemplateSyntaxError), 
    85      
     85 
    8686    # Raise TemplateSyntaxError when trying to access a filter containing an illegal character 
    8787    'basic-syntax26': ("{{ var|fil(ter) }}", {}, template.TemplateSyntaxError), 
    88      
     88 
    8989    # Raise TemplateSyntaxError for invalid block tags 
    9090    'basic-syntax27': ("{% nothing_to_see_here %}", {}, template.TemplateSyntaxError), 
    91      
     91 
    9292    # Raise TemplateSyntaxError for empty block tags 
    9393    'basic-syntax28': ("{% %}", {}, template.TemplateSyntaxError), 
    94      
    95     ### INHERITANCE TESTS ##################################################### 
     94 
     95    ### IF TAG ################################################################ 
     96    'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"), 
     97    'if-tag02': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": False}, "no"), 
     98    'if-tag03': ("{% if foo %}yes{% else %}no{% endif %}", {}, "no"), 
     99 
     100    ### COMMENT TAG ########################################################### 
     101    'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"), 
     102    'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"), 
     103 
     104    ### FOR TAG ############################################################### 
     105    'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"), 
     106    'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"), 
     107 
     108    ### IFEQUAL TAG ########################################################### 
     109    'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), 
     110    'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"), 
     111    'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"), 
     112    'ifequal04': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 1}, "yes"), 
     113 
     114    ### IFNOTEQUAL TAG ######################################################## 
     115    'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 
     116    'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""), 
     117    'ifnotequal03': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 
     118    'ifnotequal04': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 1}, "no"), 
     119 
     120    ### INHERITANCE ########################################################### 
    96121 
    97122    # Standard template with no inheritance 
     
    152177    'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'), 
    153178 
    154     ### EXCEPTION TESTS ####################################################### 
     179    ### EXCEPTIONS ############################################################ 
    155180 
    156181    # Raise exception for invalid template name 
     
    199224            failed_tests.append(name) 
    200225    template_loader.load_template_source = old_template_loader 
    201      
     226 
    202227    if failed_tests and not standalone: 
    203228        msg = "Template tests %s failed." % failed_tests 
     
    205230            msg += "  Re-run tests with -v1 to see actual failures" 
    206231        raise Exception, msg 
    207      
     232 
    208233if __name__ == "__main__": 
    209234    run_tests(1, True)