Skip to content

Commit

Permalink
chore: add_template helper for Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 17, 2023
1 parent d51ef81 commit 5925196
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions mknodes/jinja/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Environment(jinja2.Environment):
def __init__(self, *, undefined: str = "silent", load_templates: bool = False):
loader = jinjahelpers.resource_loader if load_templates else None
behavior = jinjahelpers.UNDEFINED_BEHAVIOR[undefined]
self.extra_files: set[str] = set()
super().__init__(undefined=behavior, loader=loader)
self.filters.update(ENVIRONMENT_FILTERS)
self.globals.update(ENVIRONMENT_GLOBALS)
Expand Down Expand Up @@ -91,6 +92,32 @@ def render_string(self, markdown: str, variables: dict | None = None):
logger.exception("Error when rendering template.")
return ""

def add_template(self, file: str | os.PathLike):
"""Add a new template during runtime.
Will create a new DictLoader and inject it into the existing loaders.
Useful since render_string/render_file does not allow to use a parent template.
Using this, render_template can be used.
Arguments:
file: File to add as a template
"""
# we keep track of already added extra files to not add things multiple times.
file = str(file)
if file in self.extra_files:
return
self.extra_files.add(file)
content = load_file(file)
new_loader = jinja2.DictLoader({file: content})
match self.loader:
case jinja2.ChoiceLoader():
self.loader.loaders = [new_loader, *self.loader.loaders]
case None:
self.loader = new_loader
case _:
self.loader = jinja2.ChoiceLoader(loaders=[new_loader, self.loader])

def render_file(self, file: str | os.PathLike, variables: dict | None = None) -> str:
"""Helper to directly render a template from filesystem.
Expand Down Expand Up @@ -134,11 +161,15 @@ def with_globals(self, **kwargs):


if __name__ == "__main__":
from mknodes.project import Project
# from mknodes.project import Project

env = Environment()
proj = Project.for_mknodes()
ctx = proj.context.as_dict()
env.globals.update(ctx)
text = env.render_string("{{ 'TTset' | isinstance(str) }}")
print(text)
env.set_mknodes_filters()
print(env.loader)
env.add_template("mknodes/resources/requirements.md")
env.get_template("mknodes/resources/requirements.md")
# proj = Project.for_mknodes()
# ctx = proj.context.as_dict()
# env.globals.update(ctx)
# text = env.render_string("{{ 'TTset' | isinstance(str) }}")
# print(text)

0 comments on commit 5925196

Please sign in to comment.