Use embedded templates
This commit is contained in:
parent
ff93acb65c
commit
65cce5b723
1 changed files with 19 additions and 22 deletions
|
@ -20,6 +20,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -29,42 +30,38 @@ import (
|
||||||
"github.com/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
|
|
||||||
"git.cacert.org/cacert-boardvoting/internal/models"
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
||||||
|
"git.cacert.org/cacert-boardvoting/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTemplateCache() (map[string]*template.Template, error) {
|
func newTemplateCache() (map[string]*template.Template, error) {
|
||||||
cache := map[string]*template.Template{}
|
cache := map[string]*template.Template{}
|
||||||
|
|
||||||
pages, err := filepath.Glob("./ui/html/pages/*.html")
|
pages, err := fs.Glob(ui.Files, "html/pages/*.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not find page templates: %w", err)
|
return nil, fmt.Errorf("could not find page templates: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
funcMaps := sprig.FuncMap()
|
||||||
|
funcMaps["nl2br"] = func(text string) template.HTML {
|
||||||
|
// #nosec G203 input is sanitized
|
||||||
|
return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "<br>"))
|
||||||
|
}
|
||||||
|
funcMaps["canManageUsers"] = func(*models.Voter) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
funcMaps[csrf.TemplateTag] = csrf.TemplateField
|
||||||
|
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
name := filepath.Base(page)
|
name := filepath.Base(page)
|
||||||
|
|
||||||
files := []string{
|
ts, err := template.New("").Funcs(funcMaps).ParseFS(
|
||||||
"./ui/html/base.html",
|
ui.Files,
|
||||||
"./ui/html/partials/motion_actions.html",
|
"html/base.html",
|
||||||
"./ui/html/partials/motion_display.html",
|
"html/partials/*.html",
|
||||||
"./ui/html/partials/motion_status_class.html",
|
|
||||||
"./ui/html/partials/nav.html",
|
|
||||||
"./ui/html/partials/pagination.html",
|
|
||||||
page,
|
page,
|
||||||
}
|
)
|
||||||
|
|
||||||
funcMaps := sprig.FuncMap()
|
|
||||||
funcMaps["nl2br"] = func(text string) template.HTML {
|
|
||||||
// #nosec G203 input is sanitized
|
|
||||||
return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "<br>"))
|
|
||||||
}
|
|
||||||
funcMaps["canManageUsers"] = func(*models.Voter) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
funcMaps[csrf.TemplateTag] = csrf.TemplateField
|
|
||||||
|
|
||||||
ts, err := template.New("").Funcs(funcMaps).ParseFiles(files...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse templates: %w", err)
|
return nil, fmt.Errorf("could not parse base template: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cache[name] = ts
|
cache[name] = ts
|
||||||
|
|
Loading…
Reference in a new issue