federated-dash/cmd/web/templates.go

47 lines
828 B
Go
Raw Normal View History

2024-06-25 17:35:30 +00:00
package main
import (
"html/template"
"path/filepath"
)
type templateData struct {
BaseUri string
2024-06-25 20:17:08 +00:00
AppLinks []AppLink
Tier string
2024-06-25 17:35:30 +00:00
}
func newTemplateCache() (map[string]*template.Template, error) {
cache := map[string]*template.Template{}
pages, err := filepath.Glob("./ui/html/pages/*.tmpl.html")
if err != nil {
return nil, err
}
for _, page := range pages {
//Get name of page
name := filepath.Base(page)
//Parse main template file
ts, err := template.ParseFiles("./ui/html/base.tmpl.html")
if err != nil {
return nil, err
}
//Parse partials
ts, err = ts.ParseGlob("./ui/html/partials/*.tmpl.html")
if err != nil {
return nil, err
}
//Parse page
ts, err = ts.ParseFiles(page)
if err != nil {
return nil, err
}
//set in cache
cache[name] = ts
}
return cache, nil
}