From e47d92e35c8720522636ea52b3ba104c5cbce7e6 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 18 Apr 2018 23:01:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/frame/gmvc/view.go | 13 +++++++++++- geg/frame/mvc/controller/demo/template.go | 10 +++++---- geg/other/test.go | 26 ++++++++++++----------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/g/frame/gmvc/view.go b/g/frame/gmvc/view.go index 09c3ee321..d5f31abff 100644 --- a/g/frame/gmvc/view.go +++ b/g/frame/gmvc/view.go @@ -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 } \ No newline at end of file diff --git a/geg/frame/mvc/controller/demo/template.go b/geg/frame/mvc/controller/demo/template.go index 050cbbb1e..c56a1c55e 100644 --- a/geg/frame/mvc/controller/demo/template.go +++ b/geg/frame/mvc/controller/demo/template.go @@ -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{}) +} + + + diff --git a/geg/other/test.go b/geg/other/test.go index e5773169e..06f3557d8 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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() +} \ No newline at end of file