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

Method visit_Template

src/jinja2/compiler.py:829–949  ·  view source on GitHub ↗
(
        self, node: nodes.Template, frame: t.Optional[Frame] = None
    )

Source from the content-addressed store, hash-verified

827 # -- Statement Visitors
828
829 def visit_Template(
830 self, node: nodes.Template, frame: t.Optional[Frame] = None
831 ) -> None:
832 assert frame is None, "no root frame allowed"
833 eval_ctx = EvalContext(self.environment, self.name)
834
835 from .runtime import async_exported
836 from .runtime import exported
837
838 if self.environment.is_async:
839 exported_names = sorted(exported + async_exported)
840 else:
841 exported_names = sorted(exported)
842
843 self.writeline("from jinja2.runtime import " + ", ".join(exported_names))
844
845 # if we want a deferred initialization we cannot move the
846 # environment into a local name
847 envenv = "" if self.defer_init else ", environment=environment"
848
849 # do we have an extends tag at all? If not, we can save some
850 # overhead by just not processing any inheritance code.
851 have_extends = node.find(nodes.Extends) is not None
852
853 # find all blocks
854 for block in node.find_all(nodes.Block):
855 if block.name in self.blocks:
856 self.fail(f"block {block.name!r} defined twice", block.lineno)
857 self.blocks[block.name] = block
858
859 # find all imports and import them
860 for import_ in node.find_all(nodes.ImportedName):
861 if import_.importname not in self.import_aliases:
862 imp = import_.importname
863 self.import_aliases[imp] = alias = self.temporary_identifier()
864 if "." in imp:
865 module, obj = imp.rsplit(".", 1)
866 self.writeline(f"from {module} import {obj} as {alias}")
867 else:
868 self.writeline(f"import {imp} as {alias}")
869
870 # add the load name
871 self.writeline(f"name = {self.name!r}")
872
873 # generate the root render function.
874 self.writeline(
875 f"{self.func('root')}(context, missing=missing{envenv}):", extra=1
876 )
877 self.indent()
878 self.write_commons()
879
880 # process the root
881 frame = Frame(eval_ctx)
882 if "self" in find_undeclared(node.body, ("self",)):
883 ref = frame.symbols.declare_parameter("self")
884 self.writeline(f"{ref} = TemplateReference(context)")
885 frame.symbols.analyze_node(node)
886 frame.toplevel = frame.rootlevel = True

Callers

nothing calls this directly

Calls 15

writelineMethod · 0.95
failMethod · 0.95
temporary_identifierMethod · 0.95
funcMethod · 0.95
indentMethod · 0.95
write_commonsMethod · 0.95
enter_frameMethod · 0.95
pull_dependenciesMethod · 0.95
blockvisitMethod · 0.95
leave_frameMethod · 0.95
outdentMethod · 0.95
EvalContextClass · 0.85

Tested by

no test coverage detected