feat: render template web function
This commit is contained in:
27
internal/web/templates.go
Normal file
27
internal/web/templates.go
Normal file
@ -0,0 +1,27 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, page string, data map[string]any) {
|
||||
base, err := template.ParseGlob(filepath.Join("templates", "layout", "*.html"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := base.ParseFiles(filepath.Join("templates", "pages", fmt.Sprintf("%s.html", page)))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "base", data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user