-
Notifications
You must be signed in to change notification settings - Fork 27
/
render.go
42 lines (36 loc) · 1.01 KB
/
render.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
package page
import (
"html/template"
"io"
)
// HTML render some html with added template functions to support components. This
// passes the component state to be rendered.
//
// Template functions
// - "Event" takes an event string and scopes it for the component.
func HTML(layout string, c *Component) RenderFunc {
t := template.Must(template.New("").Funcs(templateFuncs(c)).Parse(layout))
return RenderFunc(func(w io.Writer) error {
if err := t.Execute(w, c.State); err != nil {
return err
}
return nil
})
}
func templateFuncs(c *Component) template.FuncMap {
return template.FuncMap{
"Event": c.Event,
}
}
// RenderFunc a helper function to ease the rendering of nodes.
type RenderFunc func(io.Writer) error
// Render take a writer and render the func.
func (r RenderFunc) Render(w io.Writer) error {
return r(w)
}
// RenderComponent wrap a component and provide a RenderFunc.
func RenderComponent(c Component) RenderFunc {
return RenderFunc(func(w io.Writer) error {
return c.Render(w, &c)
})
}