49 lines
943 B
Go
49 lines
943 B
Go
package tpl
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"management/internal/db/model/dto"
|
|
)
|
|
|
|
type TemplateConfig struct {
|
|
Root string
|
|
Extension string
|
|
Layout string
|
|
Partial string
|
|
}
|
|
|
|
type HtmlData struct {
|
|
IsAuthenticated bool
|
|
AuthorizeUser dto.AuthorizeUser
|
|
AuthorizeMenus []*dto.OwnerMenuDto
|
|
Data any
|
|
}
|
|
|
|
func (r *render) HTML(w http.ResponseWriter, req *http.Request, tpl string, data map[string]any) {
|
|
name := strings.ReplaceAll(tpl, "/", "_")
|
|
t, ok := r.templates[name]
|
|
if !ok {
|
|
http.Error(w, "template is empty", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
hd := r.setDefaultData(req, data)
|
|
|
|
buf := new(bytes.Buffer)
|
|
err := t.ExecuteTemplate(buf, filepath.Base(tpl), hd)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = buf.WriteTo(w)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|