Initial implementation of dataloader
The intention of this is to allow access to static data stored in YAML files from within templates without having to do too much weird scaffolding inside of the generator
This commit is contained in:
parent
6a06cfcb69
commit
362938bbcd
2 changed files with 28 additions and 1 deletions
|
@ -114,7 +114,7 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
|
|||
# f"[white] => {target_path}[/white]"
|
||||
# )
|
||||
|
||||
ctx = {"site": site_config}
|
||||
ctx = {"site": site_config, "data": DataLoader(base_dir / "data")}
|
||||
_template_frontmatter(tpl_frontmatter, jinja_env, ctx)
|
||||
|
||||
if "canonicalURL" not in tpl_frontmatter and not tpl_frontmatter.get(
|
||||
|
@ -136,6 +136,7 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
|
|||
match filetype:
|
||||
case _FileType.HTML_TEMPLATE:
|
||||
if not tpl_frontmatter.get("isTemplate", True):
|
||||
# this is only done since we need to set a jinja template object for later on
|
||||
ctx["x"] = raw_tpl
|
||||
tpl = jinja_env.from_string("{{ x | safe }}")
|
||||
else:
|
||||
|
|
|
@ -87,3 +87,29 @@ def debounce(wait_time):
|
|||
return debounced
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class DataLoader:
|
||||
_base_path: Path
|
||||
_store: dict[str, any]
|
||||
|
||||
def __init__(self, base_path: str | Path):
|
||||
self._store = {}
|
||||
self._base_path = Path(base_path)
|
||||
|
||||
def __getitem__(self, key):
|
||||
print(f"DataLoader.__getitem__(self, {key=})")
|
||||
|
||||
if key in self._store:
|
||||
return self._store[key]
|
||||
|
||||
p = (self._base_path/key).with_suffix(".yml")
|
||||
|
||||
with open(p) as f:
|
||||
raw_data = f.read()
|
||||
|
||||
data = load_yaml(raw_data)
|
||||
|
||||
self._store[key] = data
|
||||
|
||||
return data
|
Loading…
Add table
Add a link
Reference in a new issue