@@ -12,16 +12,13 @@ import (
12
12
)
13
13
14
14
// TemplateManager manages the loading and rendering of HTML email templates.
15
- // 템플릿 파일은 지정한 baseDir 하위에 존재하며, 내부 플레이스홀더 (예: {{.name}}, {{.group}}, {{.email}})
16
- // 의 치환 기능을 지원합니다.
17
15
type TemplateManager struct {
18
16
templates map [string ]* template.Template
19
17
baseDir string
20
18
imageDir string
21
19
mu sync.RWMutex
22
20
}
23
21
24
- // NewTemplateManager initializes and returns a new TemplateManager with the provided base directory.
25
22
func NewTemplateManager (baseDir string , imageDir string ) * TemplateManager {
26
23
return & TemplateManager {
27
24
templates : make (map [string ]* template.Template ),
@@ -34,18 +31,13 @@ func (tm *TemplateManager) BaseDir() string {
34
31
return tm .baseDir
35
32
}
36
33
37
- // LoadTemplate loads a template file (relative to baseDir) and caches it under the given name.
38
- // 예: 이름 "default"로 "default.html" 파일을 로드하여 캐시에 저장합니다.
34
+ // LoadTemplate loads a template file and caches it under the given name.
39
35
func (tm * TemplateManager ) LoadTemplate (name , filename string ) error {
40
36
fullPath := filepath .Join (tm .baseDir , filename )
41
37
tmpl , err := template .New (filename ).Funcs (template.FuncMap {
42
- // Dummy image function that does nothing
43
- "image" : func (imageSrc string ) string {
44
- return ""
45
- },
46
- "imageWithSize" : func (imageSrc , width , height string ) string {
47
- return ""
48
- },
38
+ "image" : func (imageSrc string ) string { return "" },
39
+ "imageWithSize" : func (imageSrc , width , height string ) string { return "" },
40
+ "property" : func (key string ) string { return "" },
49
41
}).ParseFiles (fullPath )
50
42
if err != nil {
51
43
return fmt .Errorf ("failed to parse template file %s: %v" , fullPath , err )
@@ -57,7 +49,6 @@ func (tm *TemplateManager) LoadTemplate(name, filename string) error {
57
49
}
58
50
59
51
// RenderTemplate executes the cached template identified by name using the provided data.
60
- // 데이터는 예를 들어 map[string]interface{} 형태로 전달할 수 있습니다.
61
52
func (tm * TemplateManager ) RenderTemplate (name string , data interface {}) (string , []email.Attachment , error ) {
62
53
tm .mu .RLock ()
63
54
tmpl , exists := tm .templates [name ]
@@ -67,7 +58,7 @@ func (tm *TemplateManager) RenderTemplate(name string, data interface{}) (string
67
58
}
68
59
var buf bytes.Buffer
69
60
var attachments []email.Attachment
70
- tmpl .Funcs (template.FuncMap {
61
+ tmpl = tmpl .Funcs (template.FuncMap {
71
62
"image" : func (imageSrc string ) template.HTML {
72
63
imagePath := filepath .Join (tm .imageDir , imageSrc )
73
64
if _ , err := os .Stat (imagePath ); err != nil {
@@ -110,14 +101,24 @@ func (tm *TemplateManager) RenderTemplate(name string, data interface{}) (string
110
101
})
111
102
return template .HTML (fmt .Sprintf ("<img src=\" cid:%s\" alt=\" %s\" width=\" %s\" height=\" %s\" >" , imageSrc , imageSrc , width , height ))
112
103
},
104
+ "property" : func (key string ) string {
105
+ if dataMap , ok := data .(map [string ]interface {}); ok {
106
+ if custom , ok := dataMap ["custom" ].(map [string ]string ); ok {
107
+ if val , exists := custom [key ]; exists {
108
+ return val
109
+ }
110
+ }
111
+ }
112
+ return ""
113
+ },
113
114
})
115
+
114
116
if err := tmpl .Execute (& buf , data ); err != nil {
115
117
return "" , nil , fmt .Errorf ("failed to execute template %s: %v" , name , err )
116
118
}
117
119
return buf .String (), attachments , nil
118
120
}
119
121
120
- // ListTemplates returns a slice of the names of the currently loaded templates.
121
122
func (tm * TemplateManager ) ListTemplates () []string {
122
123
tm .mu .RLock ()
123
124
defer tm .mu .RUnlock ()
@@ -128,8 +129,6 @@ func (tm *TemplateManager) ListTemplates() []string {
128
129
return names
129
130
}
130
131
131
- // ExportedTemplates returns a shallow copy of the internal template map.
132
- // 외부에서는 이 복사본을 읽기 전용으로 활용할 수 있습니다.
133
132
func (tm * TemplateManager ) ExportedTemplates () map [string ]* template.Template {
134
133
tm .mu .RLock ()
135
134
defer tm .mu .RUnlock ()
@@ -140,15 +139,12 @@ func (tm *TemplateManager) ExportedTemplates() map[string]*template.Template {
140
139
return templatesCopy
141
140
}
142
141
143
- // DeleteTemplate removes the template identified by name from the cache.
144
142
func (tm * TemplateManager ) DeleteTemplate (name string ) {
145
143
tm .mu .Lock ()
146
144
defer tm .mu .Unlock ()
147
145
delete (tm .templates , name )
148
146
}
149
147
150
- // Templates returns the internal template map.
151
- // 이 메서드는 내부에서만 읽기 전용으로 사용합니다.
152
148
func (tm * TemplateManager ) Templates () map [string ]* template.Template {
153
149
return tm .ExportedTemplates ()
154
150
}
0 commit comments