22 lines
458 B
Go
22 lines
458 B
Go
|
package corelib
|
||
|
|
||
|
import (
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func TemplateHandler(w http.ResponseWriter, r *http.Request, data any, filenames ...string) {
|
||
|
// 解析模板文件
|
||
|
tmpl, err := template.ParseFiles(filenames...)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 渲染模板并写入响应
|
||
|
err = tmpl.Execute(w, data)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
}
|
||
|
}
|