This commit is contained in:
心隨緣動 2025-08-26 15:45:44 +08:00 committed by GitHub
parent f04677c810
commit c0ac4c4b7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -128,28 +128,39 @@ func (s *Server) getHtmlFiles() ([]string, error) {
} }
func (s *Server) getHtmlTemplate(funcMap template.FuncMap) (*template.Template, error) { func (s *Server) getHtmlTemplate(funcMap template.FuncMap) (*template.Template, error) {
t := template.New("").Funcs(funcMap) // 这里用 htmlFS//go:embed html/*而不是“templates”
err := fs.WalkDir(htmlFS, "html", func(path string, d fs.DirEntry, err error) error { t := template.New("").Funcs(funcMap)
if err != nil {
return err
}
if d.IsDir() { // 递归遍历 embed 的 html 目录,解析所有 .html 模板
newT, err := t.ParseFS(htmlFS, path+"/*.html") err := fs.WalkDir(htmlFS, "html", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
// ignore return err
return nil }
} if d.IsDir() {
t = newT return nil
} }
return nil if !strings.HasSuffix(path, ".html") {
}) return nil
if err != nil { }
return nil, err
} // 读出模板内容
return t, nil b, err := htmlFS.ReadFile(path)
if err != nil {
return err
}
// 去掉前缀“html/”,让 {{template "form/inbound"}} 这种名字能被正确找到
name := strings.TrimPrefix(path, "html/")
_, err = t.New(name).Parse(string(b))
return err
})
if err != nil {
return nil, err
}
return t, nil
} }
func (s *Server) initRouter() (*gin.Engine, error) { func (s *Server) initRouter() (*gin.Engine, error) {
if config.IsDebug() { if config.IsDebug() {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)