|
|
|
@ -5,11 +5,13 @@ import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"html/template"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"git.runcible.io/learning/ratchet/internal/model"
|
|
|
|
|
"git.runcible.io/learning/ratchet/ui"
|
|
|
|
|
"github.com/alexedwards/scs/v2"
|
|
|
|
|
"github.com/justinas/nosurf"
|
|
|
|
|
)
|
|
|
|
@ -123,6 +125,37 @@ func InitTemplateCache() (*TemplateCache, error) {
|
|
|
|
|
return &cache, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func InitFSTemplateCache() (*TemplateCache, error) {
|
|
|
|
|
cache := TemplateCache{}
|
|
|
|
|
|
|
|
|
|
pages, err := fs.Glob(ui.Files, "html/pages/*.tmpl")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, page := range pages {
|
|
|
|
|
name := filepath.Base(page)
|
|
|
|
|
|
|
|
|
|
// Create a slice container the filepath patterns for the templates we
|
|
|
|
|
// want to parse
|
|
|
|
|
patterns := []string{
|
|
|
|
|
"html/base.go.tmpl",
|
|
|
|
|
"html/partials/*.tmpl",
|
|
|
|
|
page,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpl, err := template.New(name).Funcs(templateFuncMap).ParseFS(ui.Files, patterns...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cache[name] = tmpl
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &cache, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func renderTemplate(w http.ResponseWriter, r *http.Request, tc *TemplateCache, status int, page string, data templateData) {
|
|
|
|
|
cache := *tc
|
|
|
|
|
ts, ok := cache[page]
|
|
|
|
|