bookworm/templates/templates.go

35 lines
678 B
Go
Raw Permalink Normal View History

2024-05-29 10:24:13 +01:00
package templates
import (
"bytes"
"embed"
"fmt"
"text/template"
)
//go:embed *.tmpl
var templates embed.FS
func GetTemplates() (*template.Template, error) {
tmpl, err := template.New("").ParseFS(templates, "*.tmpl")
if err != nil {
return nil, fmt.Errorf("cannot parse templates: %w", err)
}
return tmpl, nil
}
func Execute(data any) ([]byte, error) {
buffer := bytes.NewBufferString("")
tmpl, err := GetTemplates()
if err != nil {
return nil, fmt.Errorf("cannot get templates: %w", err)
}
if err := tmpl.ExecuteTemplate(buffer, "recipe", data); err != nil {
return nil, fmt.Errorf("cannot execute template: %w", err)
}
return buffer.Bytes(), nil
}