46 lines
857 B
Go
46 lines
857 B
Go
|
package email
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"html/template"
|
||
|
txt "text/template"
|
||
|
)
|
||
|
|
||
|
func RenderHTMLTemplate(layout, content string, data map[string]any) (string, error) {
|
||
|
// layout
|
||
|
tpl, err := template.New("layout").Parse(layout)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// content
|
||
|
_, err = tpl.New("content").Parse(content)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// execute layout + content template and RenderHTML data
|
||
|
buf := new(bytes.Buffer)
|
||
|
err = tpl.ExecuteTemplate(buf, "layout", data)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return buf.String(), nil
|
||
|
}
|
||
|
|
||
|
func RenderTxtTemplate(content string, data map[string]any) (string, error) {
|
||
|
tpl, err := txt.New("content").Parse(content)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
buf := new(bytes.Buffer)
|
||
|
err = tpl.ExecuteTemplate(buf, "content", data)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return buf.String(), nil
|
||
|
}
|