forked from qor/mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
90 lines (77 loc) · 2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package mailer
import (
"html/template"
"net/http"
"net/url"
"github.com/pkg/errors"
)
// Template email template
type Template struct {
Name string
Layout string
Data interface{}
Request *http.Request
Writer http.ResponseWriter
funcMap template.FuncMap
}
// Funcs set template's funcs
func (tmpl Template) Funcs(funcMap template.FuncMap) Template {
tmpl.funcMap = funcMap
return tmpl
}
// Render render template
func (mailer Mailer) Render(t Template) (*Email, error) {
var email Email
if t.funcMap == nil {
t.funcMap = template.FuncMap{}
}
if _, ok := t.funcMap["root_url"]; !ok {
t.funcMap["root_url"] = func() string {
if t.Request != nil && t.Request.URL != nil {
var newURL url.URL
newURL.Host = t.Request.URL.Host
newURL.Scheme = t.Request.URL.Scheme
return newURL.String()
}
return ""
}
}
if t.Layout != "" {
result, err := mailer.Config.Render.Layout(t.Layout+".text").Funcs(t.funcMap).Render(t.Name+".text", t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render layout (txt) failed")
}
email.Text = string(result)
result, err = mailer.Config.Render.Layout(t.Layout+".html").Funcs(t.funcMap).Render(t.Name+".html", t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render layout (html) failed")
}
email.HTML = string(result)
if email.HTML == "" {
result, err := mailer.Config.Render.Layout(t.Layout).Funcs(t.funcMap).Render(t.Name, t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render layout (fallback) failed")
}
email.HTML = string(result)
}
} else {
result, err := mailer.Config.Render.Funcs(t.funcMap).Render(t.Name+".text", t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render (txt) failed")
}
email.Text = string(result)
result, err = mailer.Config.Render.Funcs(t.funcMap).Render(t.Name+".html", t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render (html) failed")
}
email.HTML = string(result)
if email.HTML == "" {
result, err := mailer.Config.Render.Funcs(t.funcMap).Render(t.Name, t.Data, t.Request, t.Writer)
if err != nil {
return nil, errors.Wrap(err, "mail/render (fallback) failed")
}
email.HTML = string(result)
}
}
return &email, nil
}