From 176596b92763e8f33e32840fd94e12beee6b180d Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 26 Feb 2024 22:53:08 +0100 Subject: [PATCH] Now WebRoot also uses the new templates in the new demo. --- .../Main.tscn | 4 +- .../WebRoot.gd | 84 ++----------------- .../templates/Root.phtpl | 52 ++++++++++++ .../templates/Root.phtpl.import | 13 +++ .../templates/Root.tres | 28 +++++++ 5 files changed, 101 insertions(+), 80 deletions(-) create mode 100644 web/users_sqlite_bootstrap_templated/templates/Root.phtpl create mode 100644 web/users_sqlite_bootstrap_templated/templates/Root.phtpl.import create mode 100644 web/users_sqlite_bootstrap_templated/templates/Root.tres diff --git a/web/users_sqlite_bootstrap_templated/Main.tscn b/web/users_sqlite_bootstrap_templated/Main.tscn index 26c7b8e..501d38a 100644 --- a/web/users_sqlite_bootstrap_templated/Main.tscn +++ b/web/users_sqlite_bootstrap_templated/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=15 format=2] +[gd_scene load_steps=16 format=2] [ext_resource path="res://WebServerSimple.gd" type="Script" id=1] [ext_resource path="res://WebRoot.gd" type="Script" id=2] @@ -11,6 +11,7 @@ [ext_resource path="res://templates/RegisterDefaultPage.tres" type="HTMLTemplate" id=9] [ext_resource path="res://templates/RegisterSuccessPage.tres" type="HTMLTemplate" id=10] [ext_resource path="res://templates/SettingsPage.tres" type="HTMLTemplate" id=11] +[ext_resource path="res://templates/Root.tres" type="HTMLTemplate" id=12] [sub_resource type="SessionSetupWebServerMiddleware" id=3] @@ -31,6 +32,7 @@ script = ExtResource( 1 ) www_root_path = "res://www/" middlewares = [ SubResource( 3 ), SubResource( 4 ), SubResource( 5 ) ] script = ExtResource( 2 ) +template = ExtResource( 12 ) [node name="StaticWebPage" type="StaticWebPage" parent="WebServerSimple/WebRoot"] uri_segment = "/" diff --git a/web/users_sqlite_bootstrap_templated/WebRoot.gd b/web/users_sqlite_bootstrap_templated/WebRoot.gd index 1619db2..6ff2a3b 100644 --- a/web/users_sqlite_bootstrap_templated/WebRoot.gd +++ b/web/users_sqlite_bootstrap_templated/WebRoot.gd @@ -1,89 +1,15 @@ extends WebRoot +export(HTMLTemplate) var template : HTMLTemplate + var header : String var footer : String func _ready() -> void: - var b : HTMLBuilder = HTMLBuilder.new() - - b.meta().charset_utf_8() - b.meta().attrib("name", "viewport").attrib("content", "width=device-width, initial-scale=1, shrink-to-fit=no") - b.link().rel("stylesheet").type("text/css").href("/css/bootstrap.min.css") - b.link().rel("stylesheet").type("text/css").href("/css/main.css") - - b.write_tag() - - header = b.result - - b.result = "" - - b.cdiv() - b.ctag("main") - b.script().src("/js/jquery-3.3.1.js").f().cscript() - b.script().src("/js/popper.js").f().cscript() - b.script().src("/js/bootstrap.min.js").f().cscript() - b.write_tag() - - footer = b.result + header = template.render(null, { "type": "header" }) + footer = template.render(null, { "type": "footer" }) func _render_main_menu(request: WebServerRequest) -> void: request.head = header - - var user : User = request.get_meta("user", null) - - var b : HTMLBuilder = HTMLBuilder.new() - - b.nav().cls("navbar navbar-expand-lg navbar-light bg-light") - - if true: - b.a("/", "navbar-brand").f().w("USTB").ca() - - b.button().cls("navbar-toggler").type("button").attrib("data-toggle", "collapse").attrib("data-target", "#navbarSupportedContent").attrib("aria-controls", "navbarSupportedContent").attrib("aria-expanded", "false").attrib("aria-label", "Toggle navigation") - b.span().cls("navbar-toggler-icon").f().cspan() - b.cbutton() - - b.div("collapse navbar-collapse", "navbarSupportedContent") - - b.ul().cls("navbar-nav mr-auto") - if true: - b.li().cls("nav-item") - b.a("/", "nav-link").f().w("Index").ca() - b.cli() - - if user: - b.li().cls("nav-item") - b.a("/user/settings", "nav-link").f().w("User Settings").ca() - b.cli() - - b.li().cls("nav-item") - b.a("/user/logout", "nav-link").f().w("Logout").ca() - b.cli() - - b.li().cls("nav-item") - #b.a("/user/settings", "nav-link").f().w("(Logged in as " + user.user_name + ")!").ca() - b.a("", "nav-link").f().w("Logged in as: " + user.user_name + "!").ca() - b.cli() - - else: - b.li().cls("nav-item") - b.a("/user/login", "nav-link").f().w("Login").ca() - b.cli() - - b.li().cls("nav-item") - b.a("/user/register", "nav-link").f().w("Register").ca() - b.cli() - - b.cul() - - b.cdiv() - - b.cnav() - - b.tag("main").cls("mt-5") - - b.div("container-fluid") - - b.write_tag() - request.body += b.result - + request.body += template.render(request, { "type": "menu" }) request.footer = footer diff --git a/web/users_sqlite_bootstrap_templated/templates/Root.phtpl b/web/users_sqlite_bootstrap_templated/templates/Root.phtpl new file mode 100644 index 0000000..2c5369d --- /dev/null +++ b/web/users_sqlite_bootstrap_templated/templates/Root.phtpl @@ -0,0 +1,52 @@ + +[ Header ] + + + + + +[ Footer ] + + + + + + +[ MainMenu ] + +
+
+ +[ MainMenuLoggedIn ] + +
+
diff --git a/web/users_sqlite_bootstrap_templated/templates/Root.phtpl.import b/web/users_sqlite_bootstrap_templated/templates/Root.phtpl.import new file mode 100644 index 0000000..fc2d4f5 --- /dev/null +++ b/web/users_sqlite_bootstrap_templated/templates/Root.phtpl.import @@ -0,0 +1,13 @@ +[remap] + +importer="html_template_data" +type="HTMLTemplateData" +path="res://.import/Root.phtpl-903fa95b7c562c95d04ea3da500d65d8.res" + +[deps] + +source_file="res://templates/Root.phtpl" +dest_files=[ "res://.import/Root.phtpl-903fa95b7c562c95d04ea3da500d65d8.res" ] + +[params] + diff --git a/web/users_sqlite_bootstrap_templated/templates/Root.tres b/web/users_sqlite_bootstrap_templated/templates/Root.tres new file mode 100644 index 0000000..51afb5f --- /dev/null +++ b/web/users_sqlite_bootstrap_templated/templates/Root.tres @@ -0,0 +1,28 @@ +[gd_resource type="HTMLTemplate" load_steps=3 format=2] + +[ext_resource path="res://templates/Root.phtpl" type="HTMLTemplateData" id=1] + +[sub_resource type="GDScript" id=1] +script/source = "extends HTMLTemplate + +func _render(request: WebServerRequest, data: Dictionary) -> String: + if data[\"type\"] == \"header\": + return get_and_render_template(@\"Header\", data) + elif data[\"type\"] == \"footer\": + return get_and_render_template(@\"Footer\", data) + else: + var user : User = request.get_meta(\"user\", null) + data[\"user\"] = user + + if user: + return get_and_render_template(@\"MainMenuLoggedIn\", data) + else: + return get_and_render_template(@\"MainMenu\", data) + +" + +[resource] +templates = [ ExtResource( 1 ) ] +template_override/add_key_button = null +template_defaults/add_key_button = null +script = SubResource( 1 )