an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

81 lines
1.6 KiB

package root
import (
"embed"
"html/template"
"io/fs"
"log"
"path/filepath"
)
//go:embed templates static/*
var webAssetsFS embed.FS
var WebTemplates *template.Template
// Custom helper functions for templates
var funcMap = template.FuncMap{
"pageContent": func(name string) string {
// This allows us to reference specific content blocks
return "{{template \"" + name + "-content\" .}}"
},
}
// InitializeWebTemplates parses all HTML templates in the embedded filesystem
func InitializeWebTemplates() error {
var err error
WebTemplates, err = parseWebTemplates()
return err
}
func parseWebTemplates() (*template.Template, error) {
tmpl := template.New("").Funcs(funcMap)
// First collect all template paths
var templatePaths []string
err := fs.WalkDir(webAssetsFS, "templates", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".html" {
return nil
}
templatePaths = append(templatePaths, path)
return nil
})
if err != nil {
return nil, err
}
// Process each template
for _, path := range templatePaths {
log.Printf("Parsing template: %s", path)
content, err := webAssetsFS.ReadFile(path)
if err != nil {
return nil, err
}
filename := filepath.Base(path)
name := filename
_, err = tmpl.New(name).Parse(string(content))
if err != nil {
return nil, err
}
}
return tmpl, nil
}
// GetStaticFS provides access to the static assets filesystem for serving CSS and other static files
func GetStaticFS() (fs.FS, error) {
return fs.Sub(webAssetsFS, "static")
}