-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
62 lines (52 loc) · 1.33 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bytes"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"text/template"
)
type Template struct {
Name string `json:"name" yaml:"name"`
Text string `json:"text" yaml:"text"`
}
func AssembleTemplate(tpl *Template, msg *Message) (string, error) {
return Assemble(tpl.Name, tpl.Text, msg.Params)
}
func Assemble(name string, text string, data interface{}) (string, error) {
t, err := template.New(name).Parse(text)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = t.Execute(buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
type EmailTemplate struct {
From string `json:"from" yaml:"from"`
Subject string `json:"subject" yaml:"subject"`
Text *Template `json:"text" yaml:"text"`
Html *Template `json:"html" yaml:"html"`
}
func NewTemplateManager() *TemplateManager {
templateDirectory := "templates"
if os.Getenv("TEMPLATES") != "" {
templateDirectory = os.Getenv("TEMPLATES")
}
return &TemplateManager{TemplateDirectory: templateDirectory}
}
type TemplateManager struct {
TemplateDirectory string
}
func (tm *TemplateManager) ImportTemplate(name string) (*EmailTemplate, error) {
b, err := ioutil.ReadFile(tm.TemplateDirectory + "/" + name + ".yml")
if err != nil {
return nil, err
}
tpl := &EmailTemplate{}
err = yaml.Unmarshal(b, tpl)
return tpl, err
}