bookworm/templates/templates.go
Miguel de la Cruz c898be82de Adds recipe
2024-05-29 11:24:13 +02:00

34 lines
678 B
Go

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
}