From 2fb91f1b7b98fa7fc2b08949e71a6f46a9ca4e48 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Fri, 28 Aug 2020 19:01:52 +0200 Subject: [PATCH] implement config dir for subpages --- README.md | 12 ++++++++++-- server.py | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b306fc2..67363fe 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,16 @@ The order is again specified by the alpha-numeric order of the files. You can add new subpages in the *content*-location via a *subpages.json* file denoting an identifier as key and a HTML-template as value, like this: { - "identifiert-1" : "html-template-1.html", - "identifiert-2" : "html-template-2.html" + "identifier-1" : "html-template-1.html", + "identifier-2" : "html-template-2.html" + } + +You can also give identifier complex objects pointing to another config dir which will be parsed by *readJsonDir* and passed to *render_template* as a list of dictionary objects called *"extraConfig"*: + + { + "identifier-1" : { + "template" : "html-template-1.html", + "config-dir" : "config_dir_in_content_dir" } # Contact Page diff --git a/server.py b/server.py index ac8fce2..13b27ac 100755 --- a/server.py +++ b/server.py @@ -46,6 +46,7 @@ WRITE = "w" # subpages IDENTIFIER_PREFIX = "PAGE_" +CONFIG_POSTFIX = "_EXTRA_CONFIG" SUBPAGE_CONFIG_FILE = "subpages.json" SUBPAGE_CONTENT_DIR = "subpages/" @@ -180,7 +181,14 @@ def people(): def content(): identifier = IDENTIFIER_PREFIX + flask.request.args.get("id") if identifier in app.config: - markupText = flask.Markup(flask.render_template(app.config[identifier])) + + # check for extra config # + extraConfigDir = app.config[identifier + CONFIG_POSTFIX] + extraConfig = None + if extraConfigDir: + extraConfig = readJsonDir(os.path.join(app.config[CONTENT_DIR], extraConfigDir)) + + markupText = flask.Markup(flask.render_template(app.config[identifier], extraConfig=extraConfig)) return flask.render_template("default_content.html", conf=app.config, markupText=markupText) else: return (EMPTY_STRING, HTTP_NOT_FOUND) @@ -314,7 +322,13 @@ def init(): # set template paths for identifier in app config # for identifier in subpages.keys(): - app.config[IDENTIFIER_PREFIX + identifier] = subpages[identifier] + if type(subpages[identifier]) == dict: + app.config[IDENTIFIER_PREFIX + identifier] = subpages[identifier]["template"] + configKey = IDENTIFIER_PREFIX + identifier + CONFIG_POSTFIX + app.config[configKey] = subpages[identifier]["config-dir"] + else: + app.config[IDENTIFIER_PREFIX + identifier] = subpages[identifier] + app.config[IDENTIFIER_PREFIX + identifier + CONFIG_POSTFIX] = None # set custom loader to support second template dir # subpageContentDirTmp = os.path.join(app.config["CONTENT_DIR"], SUBPAGE_CONTENT_DIR)