2025-01-28 21:27:18 +00:00
|
|
|
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"
|
2025-01-28 21:27:18 +00:00
|
|
|
)
|
|
|
|
|
|
2025-01-28 21:37:18 +00:00
|
|
|
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
|
2025-01-28 21:27:18 +00:00
|
|
|
files := []string{
|
2025-06-25 22:43:34 +01:00
|
|
|
filepath.Join("templates", "base.tmpl.html"),
|
|
|
|
|
filepath.Join("templates", tmpl),
|
2025-01-28 21:27:18 +00:00
|
|
|
}
|
2025-01-28 21:37:18 +00:00
|
|
|
|
|
|
|
|
for _, f := range files {
|
2025-06-25 22:43:34 +01:00
|
|
|
file, err := conf.Templates.Open(f)
|
2025-01-28 21:37:18 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-06-25 22:43:34 +01:00
|
|
|
file.Close()
|
2025-01-28 21:37:18 +00:00
|
|
|
}
|
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)
|
2025-01-28 21:27:18 +00:00
|
|
|
return err
|
|
|
|
|
}
|