This commit is contained in:
John 2018-04-19 09:16:28 +08:00
commit 76bae9ab59
3 changed files with 32 additions and 17 deletions

View File

@ -76,7 +76,7 @@ func (view *View) RLockFunc(f func(vars map[string]interface{})) {
view.mu.RUnlock()
}
// 解析指定模板
// 解析并显示指定模板
func (view *View) Display(file...string) error {
name := "index.tpl"
if len(file) > 0 {
@ -89,4 +89,15 @@ func (view *View) Display(file...string) error {
view.response.Write(content)
}
return nil
}
// 解析并显示模板内容
func (view *View) DisplayContent(content string) error {
if content, err := view.ParseContent(content); err != nil {
view.response.Write("Tpl Parsing Error: " + err.Error())
return err
} else {
view.response.Write(content)
}
return nil
}

View File

@ -9,10 +9,6 @@ type ControllerTemplate struct {
gmvc.Controller
}
func init() {
ghttp.GetServer().BindControllerMethod("/template", &ControllerTemplate{}, "Info")
}
func (c *ControllerTemplate) Info() {
c.View.Assign("name", "john")
c.View.Assigns(map[string]interface{}{
@ -22,5 +18,11 @@ func (c *ControllerTemplate) Info() {
c.View.Display("user/index.tpl")
}
func init() {
ghttp.GetServer().BindController("/template", &ControllerTemplate{})
}

View File

@ -1,18 +1,20 @@
package main
import (
"fmt"
"github.com/clbanning/mxj"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/frame/gins"
)
func main() {
m := make(map[string]interface{})
m["m"] = map[string]string {
"k" : "v",
}
b, _ := mxj.Map(m).Xml()
fmt.Println(string(b))
// expect {"m":{"k":"v"}} , but I got >UNKNOWN/>
}
s := ghttp.GetServer()
s.BindHandler("/template2", func(r *ghttp.Request){
tplcontent := `id:{{.id}}, name:{{.name}}`
content, _ := gins.View().ParseContent(tplcontent, map[string]interface{}{
"id" : 123,
"name" : "john",
})
r.Response.Write(content)
})
s.SetPort(8199)
s.Run()
}