-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomponent.go
48 lines (37 loc) · 1.01 KB
/
component.go
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
package builder
import (
"bytes"
"context"
"github.com/darklab8/fl-darkstat/darkstat/common/types"
"github.com/a-h/templ"
"github.com/darklab8/go-utils/goutils/utils/utils_filepath"
"github.com/darklab8/go-utils/goutils/utils/utils_types"
)
type Component struct {
pagepath utils_types.FilePath
templ_comp templ.Component
}
func NewComponent(
pagepath utils_types.FilePath,
templ_comp templ.Component,
) *Component {
return &Component{
pagepath: pagepath,
templ_comp: templ_comp,
}
}
type WriteResult struct {
realpath utils_types.FilePath
bytes []byte
}
func (h *Component) Write(gp types.GlobalParams) WriteResult {
buf := bytes.NewBuffer([]byte{})
gp.Pagepath = string(h.pagepath)
realpath := utils_filepath.Join(gp.Buildpath, h.pagepath)
h.templ_comp.Render(context.WithValue(context.Background(), types.GlobalParamsCtxKey, gp), buf)
// Usage of gohtml is not obligatory, but nice touch simplifying debugging view.
return WriteResult{
realpath: realpath,
bytes: buf.Bytes(),
}
}