def do_if(parser, token): branches = [] bits = token.split_contents()[1:] var = TemplateIfParser(parser, bits).parse() end_nodes = (('else', 'elif', 'endif')) node_list = parser.parse(end_nodes) branches.append(IfBranch(var, node_list)) token = parser.next_token() while token.contents == "else" or token.contents.startswith("elif"): if token.contents.startswith("elif"): bits = token.split_contents()[1:] var = TemplateIfParser(parser, bits).parse() else: var = True node_list = parser.parse(end_nodes) branches.append(IfBranch(var, node_list)) token = parser.next_token() parser.delete_first_token() return IfNode(branches)
def _conditional_block(self, block, *raw_condition): if self._previous_condition_is_met: return mark_safe('') condition = TemplateIfParser(self.parser, raw_condition).parse() condition_is_met = condition.eval(self.template_context) if condition_is_met: self._previous_condition_is_met = True block_result = block.render(self.template_context) else: block_result = mark_safe('') return block_result
def do_if(parser, token): class Enders(list): def __contains__(self, val): return val.startswith('elif') or val in ('else', 'endif') enders = Enders() branches = [] while True: contents = token.split_contents() bits = contents[1:] if contents[0] in ("if", "elif"): var = TemplateIfParser(parser, bits).parse() nodelist = parser.parse(enders) next_token = parser.next_token() branches.append(IfBranch(var, nodelist)) token = next_token elif token.contents == 'else': nodelist = parser.parse(('endif', )) parser.delete_first_token() branches.append(IfBranch(True, nodelist)) break elif token.contents == 'endif': break return IfNode(branches)
def parse(parser, token, end_tag): bits = token.split_contents()[1:2] var = TemplateIfParser(parser, bits).parse() nodelist_true = parser.parse(('else', end_tag)) token = parser.next_token() if token.contents == 'else': nodelist_false = parser.parse((end_tag, )) parser.delete_first_token() else: nodelist_false = NodeList() return var, nodelist_true, nodelist_false
def parse(parser, token, end_tag): bits = token.split_contents()[1:] var_list = [] for item in bits: var_list.append(TemplateIfParser(parser, [item]).parse()) nodelist_true = parser.parse(('else', end_tag)) token = parser.next_token() if token.contents == 'else': nodelist_false = parser.parse((end_tag, )) parser.delete_first_token() else: nodelist_false = NodeList() return var_list, nodelist_true, nodelist_false
def parse_if(parser, token): bits = token.split_contents() tag_name = bits[0] end_tag = 'end%s' % tag_name condition = TemplateIfParser(parser, bits[1:]).parse() text = [] while parser.tokens: token = parser.next_token() if (token.token_type == template.TOKEN_BLOCK) and (token.contents == end_tag): return ParseIfNode(condition, u''.join(text)) if token.token_type == template.TOKEN_BLOCK: text.append(u'%s%s%s' % (template.BLOCK_TAG_START, token.contents, template.BLOCK_TAG_END)) else: text.append(token_formats[token.token_type] % token.contents) parser.unclosed_block_tag(end_tag)
def ballot(parser, token): bits = token.split_contents()[1:] var = TemplateIfParser(parser, bits).parse() return BallotNode(var)