Imported from hako
This commit is contained in:
parent
f8377df327
commit
8b8447213f
10 changed files with 315 additions and 0 deletions
36
template/engine.go
Normal file
36
template/engine.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue