-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer.go
53 lines (44 loc) · 1.3 KB
/
composer.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 components
import (
"bytes"
"html/template"
"github.com/MarErm27/GoAdmin/modules/config"
"github.com/MarErm27/GoAdmin/modules/logger"
"github.com/MarErm27/GoAdmin/modules/utils"
template2 "github.com/MarErm27/GoAdmin/template"
)
func ComposeHtml(temList map[string]string, separation bool, compo interface{}, templateName ...string) template.HTML {
tmplName := ""
if len(templateName) > 0 {
tmplName = templateName[0] + " "
}
var (
tmpl *template.Template
err error
)
if separation {
files := make([]string, 0)
root := config.GetAssetRootPath() + "pages/"
for _, v := range templateName {
files = append(files, root+temList["components/"+v]+".tmpl")
}
tmpl, err = template.New("comp").Funcs(template2.DefaultFuncMap).ParseFiles(files...)
} else {
var text = ""
for _, v := range templateName {
text += temList["components/"+v]
}
tmpl, err = template.New("comp").Funcs(template2.DefaultFuncMap).Parse(text)
}
if err != nil {
logger.Panic(tmplName + "ComposeHtml Error:" + err.Error())
return ""
}
buf := new(bytes.Buffer)
defineName := utils.ReplaceAll(templateName[0], "table/", "", "form/", "")
err = tmpl.ExecuteTemplate(buf, defineName, compo)
if err != nil {
logger.Error(tmplName+" ComposeHtml Error:", err)
}
return template.HTML(buf.String())
}