Browse Source

Func: render template to string

Vova Tkach 6 years ago
parent
commit
d37f714c76
1 changed files with 13 additions and 0 deletions
  1. 13 0
      engine/wrapper/wrapper.go

+ 13 - 0
engine/wrapper/wrapper.go

@@ -1,6 +1,7 @@
 package wrapper
 
 import (
+	"bytes"
 	"database/sql"
 	"errors"
 	"fmt"
@@ -126,6 +127,18 @@ func (this *Wrapper) MsgError(msg string) {
 		strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
 }
 
+func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {
+	tmpl, err := template.New("template").Parse(string(tcont))
+	if err != nil {
+		return err.Error()
+	}
+	var tpl bytes.Buffer
+	if err := tmpl.Execute(&tpl, data); err != nil {
+		return err.Error()
+	}
+	return tpl.String()
+}
+
 func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
 	tmpl, err := template.ParseFiles(
 		this.DTemplate+string(os.PathSeparator)+tname+".html",