-
Notifications
You must be signed in to change notification settings - Fork 6
/
render.go
51 lines (43 loc) · 1.15 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
43
44
45
46
47
48
49
50
51
package banner_editor
import (
"bytes"
"html/template"
"path/filepath"
"github.com/qor/assetfs"
"github.com/qor/qor/utils"
)
// SetAssetFS set asset fs for render
func SetAssetFS(assetFS assetfs.Interface) {
for _, viewPath := range viewPaths {
assetFS.RegisterPath(viewPath)
}
assetFileSystem = assetFS
}
// RegisterViewPath register views directory
func RegisterViewPath(p string) {
if filepath.IsAbs(p) {
viewPaths = append(viewPaths, p)
assetFileSystem.RegisterPath(p)
} else {
for _, gopath := range utils.GOPATH() {
viewPaths = append(viewPaths, filepath.Join(gopath, "src", p))
assetFileSystem.RegisterPath(filepath.Join(gopath, "src", p))
}
}
}
func render(file string, value interface{}) (template.HTML, error) {
var (
err error
content []byte
tmpl *template.Template
)
if content, err = assetFileSystem.Asset(file + ".tmpl"); err == nil {
if tmpl, err = template.New(filepath.Base(file)).Parse(string(content)); err == nil {
var result = bytes.NewBufferString("")
if err = tmpl.Execute(result, value); err == nil {
return template.HTML(result.String()), nil
}
}
}
return template.HTML(""), err
}