From d695c02223bfe98a6fb2f329d25f2a9c06ea53d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AD=90=E6=A1=93=F0=9F=8C=88?= Date: Sat, 1 Feb 2025 01:26:43 +0800 Subject: [PATCH] Refactor template directory handling Introduce a `customDir` variable to store the custom template directory path, improving code readability and reducing redundancy in both `web.go` and `email.go`. This change ensures that the custom directory is consistently referenced throughout the configuration. --- internal/cmd/web.go | 5 +++-- internal/email/email.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/cmd/web.go b/internal/cmd/web.go index 70cbeb45f..2437ed9a9 100644 --- a/internal/cmd/web.go +++ b/internal/cmd/web.go @@ -112,14 +112,15 @@ func newMacaron() *macaron.Macaron { }, )) + customDir := filepath.Join(conf.CustomDir(), "templates") renderOpt := macaron.RenderOptions{ Directory: filepath.Join(conf.WorkDir(), "templates"), - AppendDirectories: []string{filepath.Join(conf.CustomDir(), "templates")}, + AppendDirectories: []string{customDir}, Funcs: template.FuncMap(), IndentJSON: macaron.Env != macaron.PROD, } if !conf.Server.LoadAssetsFromDisk { - renderOpt.TemplateFileSystem = templates.NewTemplateFileSystem("", renderOpt.AppendDirectories[0]) + renderOpt.TemplateFileSystem = templates.NewTemplateFileSystem("", customDir) } m.Use(macaron.Renderer(renderOpt)) diff --git a/internal/email/email.go b/internal/email/email.go index 939ca411e..aad5ab598 100644 --- a/internal/email/email.go +++ b/internal/email/email.go @@ -40,9 +40,10 @@ var ( // render renders a mail template with given data. func render(tpl string, data map[string]any) (string, error) { tplRenderOnce.Do(func() { + customDir := filepath.Join(conf.CustomDir(), "templates") opt := &macaron.RenderOptions{ Directory: filepath.Join(conf.WorkDir(), "templates", "mail"), - AppendDirectories: []string{filepath.Join(conf.CustomDir(), "templates", "mail")}, + AppendDirectories: []string{filepath.Join(customDir, "mail")}, Extensions: []string{".tmpl", ".html"}, Funcs: []template.FuncMap{map[string]any{ "AppName": func() string { @@ -60,7 +61,7 @@ func render(tpl string, data map[string]any) (string, error) { }}, } if !conf.Server.LoadAssetsFromDisk { - opt.TemplateFileSystem = templates.NewTemplateFileSystem("mail", opt.AppendDirectories[0]) + opt.TemplateFileSystem = templates.NewTemplateFileSystem("mail", customDir) } ts := macaron.NewTemplateSet()