diff --git a/comm/vars.go b/comm/vars.go index 96f2cdd..707e3d5 100644 --- a/comm/vars.go +++ b/comm/vars.go @@ -4,4 +4,6 @@ import "github.com/gin-gonic/gin" var ( Gin *gin.Engine + + FileView = false ) diff --git a/main.go b/main.go index 4aee95b..8d0a2e5 100644 --- a/main.go +++ b/main.go @@ -8,13 +8,6 @@ import ( "html/template" ) -var testHTML = ` - -{{AppName}}-测试 -内容:{{.cont}} - -` - func main() { comm.Gin = gin.Default() comm.Gin.FuncMap = template.FuncMap{ @@ -22,10 +15,17 @@ func main() { return "mine app" }, } - comm.Gin.Any("/ping", func(c *gin.Context) { + //comm.FileView=true + comm.Gin.LoadHTMLGlob("view/*") + comm.Gin.Any("/test", func(c *gin.Context) { data := ruisUtil.NewMap() data.Set("cont", "你好啊world!") - utils.RenderHTML(c, testHTML, data) + utils.Render(c, "test.html", data) }) + + err := utils.InitHtmls() + if err != nil { + println("InitHtmls err:" + err.Error()) + } comm.Gin.Run(":8050") } diff --git a/utils/html.go b/utils/html.go index 079cbc7..5e8b4d5 100644 --- a/utils/html.go +++ b/utils/html.go @@ -4,12 +4,11 @@ import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/render" "gokins/comm" - "html/template" ) -func RenderHTML(c *gin.Context, htmls string, data interface{}) { - t := template.New("test").Funcs(comm.Gin.FuncMap) - /*tpe:=reflect.TypeOf(c).Elem() +func RenderHTML(c *gin.Context, name string, data interface{}) { + /*t := template.New("test").Funcs(comm.Gin.FuncMap) + tpe:=reflect.TypeOf(c).Elem() vae:=reflect.ValueOf(c).Elem() _,ok:=tpe.FieldByName("engine") if ok{ @@ -17,16 +16,24 @@ func RenderHTML(c *gin.Context, htmls string, data interface{}) { eg:=(*gin.Engine)(unsafe.Pointer(val)) //println("val:",eg) t.Funcs(eg.FuncMap) - }*/ + } _, err := t.Parse(htmls) if err != nil { c.String(500, "errs:"+err.Error()) return - } + }*/ c.Render(200, render.HTML{ - Template: t, - Name: "", + Template: HtmlSource, + Name: name, Data: data, }) } + +func Render(c *gin.Context, name string, data interface{}) { + if comm.FileView { + c.HTML(200, name, data) + } else { + RenderHTML(c, name, data) + } +} diff --git a/utils/htmls.go b/utils/htmls.go new file mode 100644 index 0000000..df7ac0e --- /dev/null +++ b/utils/htmls.go @@ -0,0 +1,40 @@ +package utils + +import ( + "gokins/comm" + "html/template" +) + +var HtmlSource = template.New("go") + +func InitHtmls() error { + HtmlSource.Funcs(comm.Gin.FuncMap) + _, err := HtmlSource.New("head.html").Parse(` + + + + +`) + if err != nil { + return err + } + + _, err = HtmlSource.New("test.html").Parse(` + + + + + {{AppName}}-测试 + {{template "head.html" .}} + + +go 内容:{{.cont}} + + +`) + if err != nil { + return err + } + + return nil +} diff --git a/view/head.html b/view/head.html new file mode 100644 index 0000000..e860f2b --- /dev/null +++ b/view/head.html @@ -0,0 +1,3 @@ + + + diff --git a/view/test.html b/view/test.html new file mode 100644 index 0000000..9e2bdbf --- /dev/null +++ b/view/test.html @@ -0,0 +1,11 @@ + + + + + {{AppName}}-测试 + {{template "head.html" .}} + + +view 内容:{{.cont}} + + \ No newline at end of file