Now WebRoot also uses the new templates in the new demo.

This commit is contained in:
Relintai 2024-02-26 22:53:08 +01:00
parent 4975c59422
commit 176596b927
5 changed files with 101 additions and 80 deletions

View File

@ -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 = "/"

View File

@ -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

View File

@ -0,0 +1,52 @@
[ Header ]
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/css/main.css">
[ Footer ]
</div>
</main>
<script src="/js/jquery-3.3.1.js"></script>
<script src="/js/popper.js"></script>
<script src="/js/bootstrap.min.js"></script>
[ MainMenu ]
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a href="/" class="navbar-brand">USTB</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="/" class="nav-link">Index</a></li>
<li class="nav-item"><a href="/user/login" class="nav-link">Login</a></li>
<li class="nav-item"><a href="/user/register" class="nav-link">Register</a></li>
</ul>
</div>
</nav>
<main class="mt-5">
<div class="container-fluid">
[ MainMenuLoggedIn ]
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a href="/" class="navbar-brand">USTB</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="/" class="nav-link">Index</a></li>
<li class="nav-item"><a href="/user/settings" class="nav-link">User Settings</a></li>
<li class="nav-item"><a href="/user/logout" class="nav-link">Logout</a></li>
<li class="nav-item"><a href="" class="nav-link">Logged in as: {{ p(user[user_name]) }}!</a></li>
</ul>
</div>
</nav>
<main class="mt-5">
<div class="container-fluid">

View File

@ -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]

View File

@ -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 )