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.
|
|
|
|
|
2024-11-15 16:12:15 +00:00
|
|
|
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
|
|
|
|
}
|