34 lines
678 B
Go
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
|
|
}
|