You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
535 B
Go
31 lines
535 B
Go
2 years ago
|
package template
|
||
2 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
2 years ago
|
goTextTemplate "text/template"
|
||
2 years ago
|
)
|
||
|
|
||
|
var _ Template = TextTemplate{}
|
||
|
|
||
|
type TextTemplate struct {
|
||
2 years ago
|
*goTextTemplate.Template
|
||
2 years ago
|
}
|
||
|
|
||
|
func NewTextTemplate(files ...string) (*TextTemplate, error) {
|
||
2 years ago
|
t, err := goTextTemplate.ParseFiles(files...)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &TextTemplate{t}, nil
|
||
|
}
|
||
|
|
||
|
func (t TextTemplate) Render(ctx map[string]any) ([]byte, error) {
|
||
2 years ago
|
var buf bytes.Buffer
|
||
|
if err := t.Template.Execute(&buf, ctx); err != nil {
|
||
2 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
2 years ago
|
return buf.Bytes(), nil
|
||
2 years ago
|
}
|