Refactor Site-Wide Data

This commit is contained in:
Chris Mann 2023-07-27 15:40:37 +02:00
parent a1ef0d09d1
commit 768ef0d836
4 changed files with 29 additions and 3 deletions

View file

@ -19,7 +19,7 @@
<body> <body>
<div class="container mb-4"> <div class="container mb-4">
<h1><a href="/">GVoisins.org > {{ template "title" .}}</a></h1> <h1><a href="/">App.LesGrandsVoisins.Com > {{ template "title" .}}</a></h1>
<nav role="navigation"> <nav role="navigation">
<div id="menuToggle"> <div id="menuToggle">
<!-- <!--

View file

@ -18,7 +18,7 @@
Nouveau mot de passe enregistré. Nouveau mot de passe enregistré.
</div> </div>
{{else}} {{else}}
<form method="POST" class="mt-4" onsubmit="javascript:var password = document.getElementById('password');var password2 = document.getElementById('password2'); if (password.value != password.value) {alert('Les maux de passe ne sont pas identiques entre l\'initial et la confirmation.');return false;}; message = checkPasswordStrength(password.value); if (message) {alert(message);return false}else{return true;}"> <form method="POST" class="mt-4" onsubmit="javascript:var password = document.getElementById('password');var password2 = document.getElementById('password2'); if (password.value != password2.value) {alert('Les maux de passe ne sont pas identiques entre l\'initial et la confirmation.');return false;}; message = checkPasswordStrength(password.value); if (message) {alert(message);return false}else{return true;}">
<div class="form-group"> <div class="form-group">
<label for="password">Nouveau mot de passe :</label> <label for="password">Nouveau mot de passe :</label>
<input type="password" id="password" name="password" class="form-control" minlength="8" required /> <input type="password" id="password" name="password" class="form-control" minlength="8" required />

View file

@ -35,6 +35,7 @@ func handleHome(w http.ResponseWriter, r *http.Request) {
LoggedIn: true, LoggedIn: true,
}, },
} }
templateHome.Execute(w, data) execTemplate(w, templateHome, &data.Common, &data.Login, *config, data)
// templateHome.Execute(w, data)
} }

25
view.go
View file

@ -5,6 +5,9 @@ package main
import ( import (
"html/template" "html/template"
"net/http"
// "net/http"
"strings" "strings"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
@ -18,6 +21,8 @@ type NestedCommonTplData struct {
LoggedIn bool LoggedIn bool
Success bool Success bool
WarningMessage string WarningMessage string
WebsiteName string
WebsiteURL string
} }
type NestedLoginTplData struct { type NestedLoginTplData struct {
Login *LoginStatus Login *LoginStatus
@ -240,6 +245,10 @@ type SendMailTplData struct {
EmailContentVars EmailContentVarsTplData EmailContentVars EmailContentVarsTplData
} }
type WrapperTemplate struct {
Template *template.Template
}
var templatePath = "./templates" var templatePath = "./templates"
func getTemplate(name string) *template.Template { func getTemplate(name string) *template.Template {
@ -250,3 +259,19 @@ func getTemplate(name string) *template.Template {
templatePath+"/"+name, templatePath+"/"+name,
)) ))
} }
type LayoutTemplateData struct {
Common NestedCommonTplData
Login NestedLoginTplData
Data any
}
func execTemplate(w http.ResponseWriter, t *template.Template, commonData *NestedCommonTplData, loginData *NestedLoginTplData, config ConfigFile, data any) error {
commonData.WebsiteURL = config.WebAddress
commonData.WebsiteName = config.Org
return t.Execute(w, LayoutTemplateData{
Common: *commonData,
Login: *loginData,
Data: data,
})
}