MCPcopy
hub / github.com/pallets/jinja / visit_Output

Method visit_Output

src/jinja2/compiler.py:1503–1580  ·  view source on GitHub ↗
(self, node: nodes.Output, frame: Frame)

Source from the content-addressed store, hash-verified

1501 self.write(")")
1502
1503 def visit_Output(self, node: nodes.Output, frame: Frame) -> None:
1504 # If an extends is active, don't render outside a block.
1505 if frame.require_output_check:
1506 # A top-level extends is known to exist at compile time.
1507 if self.has_known_extends:
1508 return
1509
1510 self.writeline("if parent_template is None:")
1511 self.indent()
1512
1513 finalize = self._make_finalize()
1514 body: t.List[t.Union[t.List[t.Any], nodes.Expr]] = []
1515
1516 # Evaluate constants at compile time if possible. Each item in
1517 # body will be either a list of static data or a node to be
1518 # evaluated at runtime.
1519 for child in node.nodes:
1520 try:
1521 if not (
1522 # If the finalize function requires runtime context,
1523 # constants can't be evaluated at compile time.
1524 finalize.const
1525 # Unless it's basic template data that won't be
1526 # finalized anyway.
1527 or isinstance(child, nodes.TemplateData)
1528 ):
1529 raise nodes.Impossible()
1530
1531 const = self._output_child_to_const(child, frame, finalize)
1532 except (nodes.Impossible, Exception):
1533 # The node was not constant and needs to be evaluated at
1534 # runtime. Or another error was raised, which is easier
1535 # to debug at runtime.
1536 body.append(child)
1537 continue
1538
1539 if body and isinstance(body[-1], list):
1540 body[-1].append(const)
1541 else:
1542 body.append([const])
1543
1544 if frame.buffer is not None:
1545 if len(body) == 1:
1546 self.writeline(f"{frame.buffer}.append(")
1547 else:
1548 self.writeline(f"{frame.buffer}.extend((")
1549
1550 self.indent()
1551
1552 for item in body:
1553 if isinstance(item, list):
1554 # A group of constant data to join and output.
1555 val = self._output_const_repr(item)
1556
1557 if frame.buffer is None:
1558 self.writeline("yield " + val)
1559 else:
1560 self.writeline(val + ",")

Callers

nothing calls this directly

Calls 11

writelineMethod · 0.95
indentMethod · 0.95
_make_finalizeMethod · 0.95
_output_const_reprMethod · 0.95
newlineMethod · 0.95
_output_child_preMethod · 0.95
_output_child_postMethod · 0.95
writeMethod · 0.95
outdentMethod · 0.95
visitMethod · 0.80

Tested by

no test coverage detected