gonotes/internal/templ/templ.go

39 lines
771 B
Go
Raw Normal View History

package templ
import (
"html/template"
"net/http"
"path/filepath"
2025-06-01 21:27:08 +01:00
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
2025-07-30 13:41:03 +01:00
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
)
type Ctx map[string]any
2025-07-30 13:41:03 +01:00
func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, context Ctx) error {
2025-06-27 22:56:50 +01:00
var err error
files := []string{
2025-06-25 22:43:34 +01:00
filepath.Join("templates", "base.tmpl.html"),
filepath.Join("templates", tmpl),
}
for _, f := range files {
2025-06-25 22:43:34 +01:00
file, err := conf.Templates.Open(f)
if err != nil {
return err
}
2025-06-25 22:43:34 +01:00
file.Close()
}
2025-06-25 22:43:34 +01:00
t, err := template.ParseFS(conf.Templates, files...)
2025-07-30 13:41:03 +01:00
if err != nil {
return err
}
context["user"] = r.Context().Value(middleware.ContextKey("user")).(string)
2025-06-27 22:56:50 +01:00
err = t.ExecuteTemplate(w, "base", context)
return err
}