rano/mailer/message/render.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-11-18 15:23:13 +00:00
// Copyright 2024 Patial Tech. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
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
}