我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用jinja2.nodes.CondExpr()。
def eval_condition(self, condition): # exceptions are handled by the caller parser = BooleanParser(env, condition, state='variable') expr = parser.parse_expression() if not parser.stream.eos: raise ValueError('chunk after expression') def eval_node(node): if isinstance(node, nodes.CondExpr): if eval_node(node.test): return eval_node(node.expr1) else: return eval_node(node.expr2) elif isinstance(node, nodes.And): return eval_node(node.left) and eval_node(node.right) elif isinstance(node, nodes.Or): return eval_node(node.left) or eval_node(node.right) elif isinstance(node, nodes.Not): return not eval_node(node.node) elif isinstance(node, nodes.Name): return self.tags.get(node.name, False) else: raise ValueError('invalid node, check parsing') return eval_node(expr)
def parse_condexpr(self): lineno = self.stream.current.lineno expr1 = self.parse_or() while self.stream.skip_if('name:if'): expr2 = self.parse_or() if self.stream.skip_if('name:else'): expr3 = self.parse_condexpr() else: expr3 = None expr1 = nodes.CondExpr(expr2, expr1, expr3, lineno=lineno) lineno = self.stream.current.lineno return expr1