homebox/backend/pkgs/mailer/templates.go
Hayden bd321af29f
chore: developer cleanup (#300)
* new PR tasks

* add homebox to know words

* formatting

* bump deps

* generate db models

* ts errors

* drop id

* fix accessor

* drop unused time field

* change CI

* add expected error

* add type check

* resolve serveral type errors

* hoise in CI
2023-02-17 21:41:01 -09:00

61 lines
1.2 KiB
Go

package mailer
import (
"bytes"
_ "embed"
"html/template"
)
//go:embed templates/welcome.html
var templatesWelcome string
type TemplateDefaults struct {
CompanyName string
CompanyAddress string
CompanyURL string
ActivateAccountURL string
UnsubscribeURL string
}
type TemplateProps struct {
Defaults TemplateDefaults
Data map[string]string
}
func (tp *TemplateProps) Set(key, value string) {
tp.Data[key] = value
}
func DefaultTemplateData() TemplateProps {
return TemplateProps{
Defaults: TemplateDefaults{
CompanyName: "Haybytes.com",
CompanyAddress: "123 Main St, Anytown, CA 12345",
CompanyURL: "https://haybytes.com",
ActivateAccountURL: "https://google.com",
UnsubscribeURL: "https://google.com",
},
Data: make(map[string]string),
}
}
func render(tpl string, data TemplateProps) (string, error) {
tmpl, err := template.New("name").Parse(tpl)
if err != nil {
return "", err
}
var tplBuffer bytes.Buffer
err = tmpl.Execute(&tplBuffer, data)
if err != nil {
return "", err
}
return tplBuffer.String(), nil
}
func RenderWelcome() (string, error) {
return render(templatesWelcome, DefaultTemplateData())
}