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)