-
Notifications
You must be signed in to change notification settings - Fork 56
/
partial_helper.go
53 lines (43 loc) · 1.16 KB
/
partial_helper.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
49
50
51
52
53
package plush
import (
"fmt"
"html/template"
"path/filepath"
"strings"
)
// PartialFeeder is callback function should implemented on application side.
type PartialFeeder func(string) (string, error)
func PartialHelper(name string, data map[string]interface{}, help HelperContext) (template.HTML, error) {
if help.Context == nil {
return "", fmt.Errorf("invalid context. abort")
}
help.Context = help.New()
for k, v := range data {
help.Set(k, v)
}
pf, ok := help.Value("partialFeeder").(func(string) (string, error))
if !ok {
return "", fmt.Errorf("could not found partial feeder from helpers")
}
var part string
var err error
if part, err = pf(name); err != nil {
return "", err
}
if part, err = Render(part, help.Context); err != nil {
return "", err
}
if ct, ok := help.Value("contentType").(string); ok {
ext := filepath.Ext(name)
if strings.Contains(ct, "javascript") && ext != ".js" && ext != "" {
part = template.JSEscapeString(string(part))
}
}
if layout, ok := data["layout"].(string); ok {
return PartialHelper(
layout,
map[string]interface{}{"yield": template.HTML(part)},
help)
}
return template.HTML(part), err
}