-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
of.go
35 lines (30 loc) · 819 Bytes
/
of.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
package content
import (
"errors"
"html/template"
"github.com/gobuffalo/helpers/hctx"
)
// ContentOf retrieves a stored block for templating and renders it.
// You can pass an optional map of fields that will be set.
/*
<%= contentOf("buttons") %>
<%= contentOf("buttons", {"label": "Click me"}) %>
*/
func ContentOf(name string, data hctx.Map, help hctx.HelperContext) (template.HTML, error) {
fn, ok := help.Value("contentFor:" + name).(func(data hctx.Map) (template.HTML, error))
if !ok {
if !help.HasBlock() {
return template.HTML(""), errors.New("missing contentOf block: " + name)
}
hc := help.New()
for k, v := range data {
hc.Set(k, v)
}
body, err := help.BlockWith(hc)
if err != nil {
return template.HTML(""), err
}
return template.HTML(body), nil
}
return fn(data)
}