-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
53 lines (43 loc) · 1.16 KB
/
render.go
File metadata and controls
53 lines (43 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package commo
import (
"bytes"
"fmt"
)
// Render returns the text and html executed templates for the specified name
// and data. Ensure that the extension is not supplied to the render method.
func Render(name string, data any) (text, html []byte, err error) {
if text, err = render(name+".txt", data); err != nil {
return nil, nil, err
}
if html, err = render(name+".html", data); err != nil {
return nil, nil, err
}
return text, html, nil
}
// Render returns the text and html executed templates as strings for the
// specified name and data. Ensure that the extension is not supplied to the
// render method.
func RenderString(name string, data any) (text, html string, err error) {
var (
tb []byte
hb []byte
)
if tb, hb, err = Render(name, data); err != nil {
return "", "", nil
}
return string(tb), string(hb), nil
}
func render(name string, data any) (_ []byte, err error) {
if templs == nil {
return nil, ErrTemplatesNotLoaded
}
t, ok := templs[name]
if !ok {
return nil, fmt.Errorf("could not find %q in templates", name)
}
buf := &bytes.Buffer{}
if err = t.Execute(buf, data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}