41 lines
888 B
Go
41 lines
888 B
Go
package templ
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes"
|
|
)
|
|
|
|
type Ctx map[string]any
|
|
|
|
func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, context Ctx) error {
|
|
var err error
|
|
files := []string{
|
|
filepath.Join("templates", "base.tmpl.html"),
|
|
filepath.Join("templates", tmpl),
|
|
}
|
|
|
|
for _, f := range files {
|
|
file, err := conf.Templates.Open(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file.Close()
|
|
}
|
|
|
|
t, err := template.ParseFS(conf.Templates, files...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
|
context["user"] = user
|
|
context["userTags"] = notes.Notes.UserTags(user)
|
|
|
|
err = t.ExecuteTemplate(w, "base", context)
|
|
return err
|
|
}
|