From 5125ad3eba795cb7bcad74505793d963efa6a521 Mon Sep 17 00:00:00 2001 From: ditaajipratama Date: Sat, 13 Jan 2024 12:01:14 +0700 Subject: [PATCH] Updating templating system --- config/directory.py | 17 +++---- config/globalvar.py | 7 ++- core/templatestaticdir.py | 9 ++++ handler.py | 4 +- modules/public/home.py | 50 ++++++++----------- .../html/footer.html | 2 +- templates/bare/html/template.html | 20 ++++++++ .../html/topnav.html | 2 +- templates/{basic_bootstrap => bare}/main.py | 11 ++-- .../static/css/style.css | 0 .../static/lib/bootstrap/LICENSE | 0 .../lib/bootstrap/css/bootstrap.min.css | 0 .../lib/bootstrap/css/bootstrap.min.css.map | 0 .../lib/bootstrap/js/bootstrap.bundle.min.js | 0 .../bootstrap/js/bootstrap.bundle.min.js.map | 0 .../static/lib/jquery/jquery-3.7.0.min.js | 0 templates/basic_bootstrap/html/template.html | 20 -------- 17 files changed, 70 insertions(+), 72 deletions(-) create mode 100644 core/templatestaticdir.py rename templates/{basic_bootstrap => bare}/html/footer.html (68%) create mode 100644 templates/bare/html/template.html rename templates/{basic_bootstrap => bare}/html/topnav.html (92%) rename templates/{basic_bootstrap => bare}/main.py (62%) rename templates/{basic_bootstrap => bare}/static/css/style.css (100%) rename templates/{basic_bootstrap => bare}/static/lib/bootstrap/LICENSE (100%) rename templates/{basic_bootstrap => bare}/static/lib/bootstrap/css/bootstrap.min.css (100%) rename templates/{basic_bootstrap => bare}/static/lib/bootstrap/css/bootstrap.min.css.map (100%) rename templates/{basic_bootstrap => bare}/static/lib/bootstrap/js/bootstrap.bundle.min.js (100%) rename templates/{basic_bootstrap => bare}/static/lib/bootstrap/js/bootstrap.bundle.min.js.map (100%) rename templates/{basic_bootstrap => bare}/static/lib/jquery/jquery-3.7.0.min.js (100%) delete mode 100644 templates/basic_bootstrap/html/template.html diff --git a/config/directory.py b/config/directory.py index cf7e7c3..550c47a 100644 --- a/config/directory.py +++ b/config/directory.py @@ -1,7 +1,5 @@ import os - -# template import -import templates.basic_bootstrap.main as basic_bootstrap +from core import templatestaticdir # pages directory page = { @@ -21,11 +19,12 @@ dirconfig = { 'tools.staticdir.on' : True , 'tools.staticdir.dir' : './static/css' , }, + '/js' : + { + 'tools.staticdir.on' : True , + 'tools.staticdir.dir' : './static/js' , + }, } -def add(template): - for row in template: - dirconfig[ row['name'] ] = row['value'] - -# template staticdir -add(basic_bootstrap.static) +# template staticdir: dirconfig dirtemplate +templatestaticdir.add(dirconfig, "templates") diff --git a/config/globalvar.py b/config/globalvar.py index fb6a5c0..c46ffbe 100755 --- a/config/globalvar.py +++ b/config/globalvar.py @@ -1,8 +1,7 @@ -GV_base_url = "http://localhost:81" -GV_title = "CostaPy" -GV_copyright = "Dita Aji Pratama" +baseurl = "http://localhost:81" +title = "CostaPy" -GV_menu = { +menu = { "public": { "topnav": [ { diff --git a/core/templatestaticdir.py b/core/templatestaticdir.py new file mode 100644 index 0000000..9327389 --- /dev/null +++ b/core/templatestaticdir.py @@ -0,0 +1,9 @@ +import os + +def add(dirconfig, template_directory): + template_directory = "templates" + template_list = [d for d in os.listdir(template_directory) if os.path.isdir(os.path.join(template_directory, d))] + for template_name in template_list: + template_module = __import__(f"{template_directory}.{template_name}.main", fromlist=["static"]) + for static in getattr(template_module, "static", []): + dirconfig[ static['name'] ] = static['value'] diff --git a/handler.py b/handler.py index 025f94c..a0cb5f5 100755 --- a/handler.py +++ b/handler.py @@ -10,6 +10,8 @@ import modules.public.home as public_home class handler(): def index(self, **kwargs): - kwargs["mako_website"] = basic_bootstrap.main(directory.page["public"], "home") + kwargs["mako"] = { + "website" : bare.main(directory.page["public"], "home") + } return public_home.main().html(kwargs) index.exposed = True diff --git a/modules/public/home.py b/modules/public/home.py index c4a7389..51a4639 100644 --- a/modules/public/home.py +++ b/modules/public/home.py @@ -3,35 +3,25 @@ import config.globalvar as globalvar class main: - def __init__(self): - pass + def __init__(self): + pass - def html(self, params): - - interface_template = params["mako_website"]['template' ] - topnav = params["mako_website"]['topnav' ] - footer = params["mako_website"]['footer' ] - container = params["mako_website"]['container' ] - - name = "World" - - user_roles = ["guest"] - active_page = "Home" - - return Template(interface_template).render( - GV_title = globalvar.GV_title, - GV_base_url = globalvar.GV_base_url, - topnav = Template(topnav).render( - GV_title = globalvar.GV_title, - menu = globalvar.GV_menu['public']['topnav'], - user_roles = user_roles, - active_page = active_page - ), - footer = Template(footer).render( - copyright_holder = globalvar.GV_copyright, - ), - container = Template(container).render( - GV_base_url = globalvar.GV_base_url, - greeting = "Hello " + name + ", " + "Welcome to " + globalvar.GV_title - ) + def html(self, params): + return Template(params["mako"]["website"]['template']).render( + title = globalvar.title, + baseurl = globalvar.baseurl, + topnav = Template(params["mako"]["website"]['topnav']).render( + title = globalvar.title, + baseurl = globalvar.baseurl, + menu = globalvar.menu['public']['topnav'], + user_roles = ["guest"], + active_page = "Home" + ), + footer = Template(params["mako"]["website"]['footer']).render( + copyright = "Dita Aji Pratama", + ), + container = Template(params["mako"]["website"]['container']).render( + baseurl = globalvar.baseurl, + greeting = f"Hello world, welcome to {globalvar.title}" + ) ) diff --git a/templates/basic_bootstrap/html/footer.html b/templates/bare/html/footer.html similarity index 68% rename from templates/basic_bootstrap/html/footer.html rename to templates/bare/html/footer.html index d26197a..0cc51ae 100644 --- a/templates/basic_bootstrap/html/footer.html +++ b/templates/bare/html/footer.html @@ -1,3 +1,3 @@ diff --git a/templates/bare/html/template.html b/templates/bare/html/template.html new file mode 100644 index 0000000..f433c3b --- /dev/null +++ b/templates/bare/html/template.html @@ -0,0 +1,20 @@ + + + + + + + ${title} + + + + + + + ${topnav} +
+ ${container} +
+ ${footer} + + diff --git a/templates/basic_bootstrap/html/topnav.html b/templates/bare/html/topnav.html similarity index 92% rename from templates/basic_bootstrap/html/topnav.html rename to templates/bare/html/topnav.html index 5313350..b243085 100644 --- a/templates/basic_bootstrap/html/topnav.html +++ b/templates/bare/html/topnav.html @@ -1,5 +1,5 @@