From 00e3ef6987558b2ce9a3634c909c29427b9d52ad Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Thu, 12 May 2022 15:42:37 +0200 Subject: [PATCH] Add for_each and template_if_empty --- makesite.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/makesite.py b/makesite.py index d866ca9..6e3b14f 100644 --- a/makesite.py +++ b/makesite.py @@ -124,8 +124,40 @@ def read_content(filename): def render(template, **params): """Replace placeholders in template with values from params.""" - return re.sub(r'{{\s*([^}\s]+)\s*}}', - lambda match: str(params.get(match.group(1), match.group(0))), + def handle_variable(match): + var = match.group(1).strip() + if match.group(1).startswith("template "): + import_name = var[7:] + import_ = fread(import_name) + return render(import_, **params) + elif var.startswith("for_each "): + args = var.split(" ")[1:] + attribute_name = args[0] + template_name = args[1] + + template_ = fread(template_name) + templates = [] + for val in params.get(attribute_name, []): + templates.append(render(template_, **params, item=val)) + return "\n".join(templates) + elif var.startswith("item."): + return str(params["item"].get(var[5:], match.group(0))) + elif var.startswith("template_if_empty "): + # {{ template_if_empty