feat: html static templates

This commit is contained in:
ange 2024-12-14 08:34:47 +00:00
parent 9d0da00371
commit e9fc659a5a
Signed by: ange
GPG key ID: 9E0C4157BB7BEB1D
12 changed files with 91 additions and 100 deletions

29
src/tmpl.go Normal file
View file

@ -0,0 +1,29 @@
package main
import (
"bytes"
"html/template"
"path/filepath"
"regexp"
)
var TMPL map[string][]byte
// TODO not use a global
// TODO use range instead of if else in header.html
func generateTmpl() {
files, _ := filepath.Glob("html/*.html")
re := regexp.MustCompile("html(/.+).html")
TMPL = make(map[string][]byte, len(files))
for _, f := range files {
b := new(bytes.Buffer)
name := re.FindStringSubmatch(f)[1]
t, _ := template.ParseFiles(f)
t.ParseGlob("tmpl/*.tmpl")
t.Execute(b, map[string]any{
"name": name,
})
TMPL[name] = b.Bytes()
}
}