2020-07-10 13:34:39 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
2021-05-12 04:54:46 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-07-10 13:34:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Render(tpl string, args interface{}) string {
|
|
|
|
var buf = bytes.NewBuffer(nil)
|
|
|
|
tmpl, err := template.New("tmp").Parse(tpl)
|
|
|
|
if err != nil {
|
2021-05-12 04:54:46 +00:00
|
|
|
logrus.WithError(err).Error("template parse error")
|
2020-07-10 13:34:39 +00:00
|
|
|
return ""
|
|
|
|
}
|
2021-05-12 04:54:46 +00:00
|
|
|
|
2020-07-10 13:34:39 +00:00
|
|
|
err = tmpl.Execute(buf, args)
|
|
|
|
if err != nil {
|
2021-05-12 04:54:46 +00:00
|
|
|
logrus.WithError(err).Error("template execute error")
|
2020-07-10 13:34:39 +00:00
|
|
|
return ""
|
|
|
|
}
|
2021-05-12 04:54:46 +00:00
|
|
|
|
2020-07-10 13:34:39 +00:00
|
|
|
return buf.String()
|
|
|
|
}
|