-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
38 lines (30 loc) · 901 Bytes
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package config
import (
"fmt"
"html/template"
"net/http"
"path"
)
func Render(w http.ResponseWriter, page string, data map[string]any, partials ...string) error {
var t *template.Template
var err error
if len(partials) > 0 {
partialPaths := make([]string, len(partials))
for i, partial := range partials {
partialPaths[i] = path.Join("./views/components", fmt.Sprintf("%s.gohtml", partial))
}
templateFiles := append(partialPaths, path.Join("./views/pages", fmt.Sprintf("%s.gohtml", page)), path.Join("./views/index.gohtml"))
t, err = template.ParseFiles(templateFiles...)
} else {
t, err = template.ParseFiles(path.Join("./views/pages", fmt.Sprintf("%s.gohtml", page)), path.Join("./views/index.gohtml"))
}
if err != nil {
http.Error(w, "Page not found", http.StatusNotFound)
return err
}
err = t.Execute(w, data)
if err != nil {
return err
}
return nil
}