51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
// Copyright 2024 Patial Tech (Ankit Patial).
|
|
//
|
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
|
// See http://opensource.org/licenses/MIT
|
|
|
|
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
|
|
}
|