File size: 1,348 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
package components

import (
	"bytes"
	"html/template"

	"github.com/GoAdminGroup/go-admin/modules/config"
	"github.com/GoAdminGroup/go-admin/modules/logger"
	"github.com/GoAdminGroup/go-admin/modules/utils"
	template2 "github.com/GoAdminGroup/go-admin/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())
}