Imported from hako

This commit is contained in:
Felipe M 2024-11-20 23:21:03 +01:00
parent f8377df327
commit 8b8447213f
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
10 changed files with 315 additions and 0 deletions

36
template/engine.go Normal file
View file

@ -0,0 +1,36 @@
package template
import (
"bytes"
"embed"
"fmt"
"html/template"
)
// Engine is a template engine
type Engine struct {
templates *template.Template
}
// Render renders the template with the given name and data
func (e *Engine) Render(name string, data any) ([]byte, error) {
var buf bytes.Buffer
if err := e.templates.ExecuteTemplate(&buf, name, data); err != nil {
return nil, fmt.Errorf("failed to execute template: %w", err)
}
return buf.Bytes(), nil
}
// NewTemplateEngine creates a new template engine from the given templates
func NewEngine(templates embed.FS) (*Engine, error) {
tmpls, err := template.ParseFS(templates, "**/*.html")
if err != nil {
return nil, fmt.Errorf("failed to parse templates: %w", err)
}
return &Engine{
templates: tmpls,
}, nil
}