elif for smart if tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def smart_if(parser, token):
    if_elifs = []
    if_spelling = 'if'
    
    class Enders(list):
        def __contains__(self, val):
            return val.startswith('elif') or val in ['else', 'endif']
    enders = Enders()
                
    while True:
        contents = token.split_contents()
        command = contents[0]
        bits = contents[1:]
        if command == if_spelling:
            var = TemplateIfParser(parser, bits).parse()
            nodelist = parser.parse(enders)
            next_token = parser.next_token()
            if_elifs.append((var, nodelist, token))
            if_spelling = 'elif'
            token = next_token
        elif token.contents == 'else':
            nodelist_false = parser.parse(('endif',))
            parser.delete_first_token()
            break
        elif token.contents == 'endif':
            nodelist_false = None
            break
    while len(if_elifs) > 1:
        var, nodelist_true, token = if_elifs.pop()
        false_node = SmartIfNode(var, nodelist_true, nodelist_false)
        nodelist_false = parser.create_nodelist()
        parser.extend_nodelist(nodelist_false, false_node, token)
    var, nodelist_true, token = if_elifs[0]
    return SmartIfNode(var, nodelist_true, nodelist_false)

More like this

  1. Django 1.2 template tag {% IF %} with {% ELIF %} support by danilchenko 3 years, 1 month ago
  2. PyIfTag - Just like python if expression by limodou 7 years, 1 month ago
  3. in_list filter by guychi 6 years, 7 months ago
  4. Counter model - run multiple persistent counters by simon 4 years, 10 months ago
  5. Pagination Alphabetically compatible with paginator_class by vascop 1 year, 12 months ago

Comments

(Forgotten your password?)