basic common app packages
This commit is contained in:
45
email/render.go
Normal file
45
email/render.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user