rano/mailer/message/render.go

49 lines
934 B
Go
Raw Normal View History

package message
import (
"bytes"
_ "embed"
"html/template"
"gitserver.in/patialtech/rano/config"
"gitserver.in/patialtech/rano/mailer"
"gitserver.in/patialtech/rano/util/structs"
)
//go:embed _layout.html
var layout string
type TplData struct {
WebAssetsURL string
mailer.Template
}
// render data in give HTML layout and content templates
func render(layout string, content string, data mailer.Template) (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
}
// excute layout + content temaplte and render data
buf := new(bytes.Buffer)
d := structs.Map(data)
d["Title"] = "My App"
d["WebAssetsURL"] = config.Read().WebURL
d["AllowReply"] = false
err = tpl.ExecuteTemplate(buf, "layout", d)
if err != nil {
return "", err
}
return buf.String(), nil
}