From fa256aec9f6f3dd5f853fe029c6ef496939c86f9 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 27 Feb 2019 22:53:39 +0800 Subject: [PATCH] add example for layout using template engine; fix issue with config error output in gview when no config used --- g/net/ghttp/ghttp_response_view.go | 1 - g/os/gcfg/gcfg.go | 30 ++++++++++++++----- g/os/gview/gview.go | 8 +++++ geg/net/ghttp/server/template/layout/main.go | 23 ++++++++++++++ .../template/layout/template/footer.html | 1 + .../template/layout/template/header.html | 1 + .../template/layout/template/layout.html | 3 ++ .../template/layout/template/main/main1.html | 1 + .../template/layout/template/main/main2.html | 1 + 9 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 geg/net/ghttp/server/template/layout/main.go create mode 100644 geg/net/ghttp/server/template/layout/template/footer.html create mode 100644 geg/net/ghttp/server/template/layout/template/header.html create mode 100644 geg/net/ghttp/server/template/layout/template/layout.html create mode 100644 geg/net/ghttp/server/template/layout/template/main/main1.html create mode 100644 geg/net/ghttp/server/template/layout/template/main/main2.html diff --git a/g/net/ghttp/ghttp_response_view.go b/g/net/ghttp/ghttp_response_view.go index acb1c30a9..8b2b63eb1 100644 --- a/g/net/ghttp/ghttp_response_view.go +++ b/g/net/ghttp/ghttp_response_view.go @@ -65,7 +65,6 @@ func (r *Response) buildInVars(params map[string]interface{}) map[string]interfa if params == nil { params = make(map[string]interface{}) } - c := gins.Config() if c.GetFilePath() != "" { params["Config"] = c.GetMap("") diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 7a606b85b..906a8eff8 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -63,13 +63,8 @@ func (c *Config) filePath(file...string) (path string) { } c.paths.RLockFunc(func(array []string) { for _, v := range array { - //fmt.Println("search:", v, name) if path, _ = gspath.Search(v, name); path != "" { break - } else { - //if strings.EqualFold(v, "/Users/john/Temp/config") { - // gutil.Dump(gspath.Get(v).AllPaths()) - //} } } }) @@ -94,6 +89,10 @@ func (c *Config) SetPath(path string) error { glog.Error(fmt.Sprintf(`[gcfg] SetPath failed: %s`, err.Error())) return err } + // 重复判断 + if c.paths.Search(realPath) != -1 { + return nil + } c.jsons.Clear() c.paths.Clear() c.paths.Append(realPath) @@ -116,14 +115,29 @@ func (c *Config) AddPath(path string) error { glog.Error(fmt.Sprintf(`[gcfg] AddPath failed: %s`, err.Error())) return err } + // 重复判断 + if c.paths.Search(realPath) != -1 { + return nil + } c.paths.Append(realPath) glog.Debug("[gcfg] AddPath:", realPath) return nil } -// 获取指定文件的绝对路径,默认获取默认的配置文件路径 -func (c *Config) GetFilePath(file...string) string { - return c.filePath(file...) +// 获取指定文件的绝对路径,默认获取默认的配置文件路径,当指定的配置文件不存在时,返回空字符串,并且不会报错。 +func (c *Config) GetFilePath(file...string) (path string) { + name := c.name.Val() + if len(file) > 0 { + name = file[0] + } + c.paths.RLockFunc(func(array []string) { + for _, v := range array { + if path, _ = gspath.Search(v, name); path != "" { + break + } + } + }) + return } // 设置配置管理对象的默认文件名称 diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index ba813bcd1..ee51bda83 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -115,6 +115,10 @@ func (view *View) SetPath(path string) error { glog.Error(fmt.Sprintf(`[gview] SetPath failed: %s`, err.Error())) return err } + // 重复判断 + if view.paths.Search(realPath) != -1 { + return nil + } view.paths.Clear() view.paths.Append(realPath) glog.Debug("[gview] SetPath:", realPath) @@ -132,6 +136,10 @@ func (view *View) AddPath(path string) error { glog.Error(fmt.Sprintf(`[gview] AddPath failed: %s`, err.Error())) return err } + // 重复判断 + if view.paths.Search(realPath) != -1 { + return nil + } view.paths.Append(realPath) glog.Debug("[gview] AddPath:", realPath) return nil diff --git a/geg/net/ghttp/server/template/layout/main.go b/geg/net/ghttp/server/template/layout/main.go new file mode 100644 index 000000000..809376b66 --- /dev/null +++ b/geg/net/ghttp/server/template/layout/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "github.com/gogf/gf/g" + "github.com/gogf/gf/g/net/ghttp" +) + +func main() { + s := g.Server() + s.BindHandler("/main1", func(r *ghttp.Request) { + r.Response.WriteTpl("layout.html", g.Map{ + "mainTpl" : "main/main1.html", + }) + }) + s.BindHandler("/main2", func(r *ghttp.Request) { + r.Response.WriteTpl("layout.html", g.Map{ + "mainTpl" : "main/main2.html", + }) + }) + g.View().SetPath("template") + s.SetPort(8199) + s.Run() +} diff --git a/geg/net/ghttp/server/template/layout/template/footer.html b/geg/net/ghttp/server/template/layout/template/footer.html new file mode 100644 index 000000000..83ae25b65 --- /dev/null +++ b/geg/net/ghttp/server/template/layout/template/footer.html @@ -0,0 +1 @@ +

FOOTER

\ No newline at end of file diff --git a/geg/net/ghttp/server/template/layout/template/header.html b/geg/net/ghttp/server/template/layout/template/header.html new file mode 100644 index 000000000..b9cb0a77c --- /dev/null +++ b/geg/net/ghttp/server/template/layout/template/header.html @@ -0,0 +1 @@ +

HEADER

\ No newline at end of file diff --git a/geg/net/ghttp/server/template/layout/template/layout.html b/geg/net/ghttp/server/template/layout/template/layout.html new file mode 100644 index 000000000..9f58c21a0 --- /dev/null +++ b/geg/net/ghttp/server/template/layout/template/layout.html @@ -0,0 +1,3 @@ +{{include "header.html" .}} +{{include .mainTpl .}} +{{include "footer.html" .}} \ No newline at end of file diff --git a/geg/net/ghttp/server/template/layout/template/main/main1.html b/geg/net/ghttp/server/template/layout/template/main/main1.html new file mode 100644 index 000000000..fdb0016f3 --- /dev/null +++ b/geg/net/ghttp/server/template/layout/template/main/main1.html @@ -0,0 +1 @@ +

MAIN1

\ No newline at end of file diff --git a/geg/net/ghttp/server/template/layout/template/main/main2.html b/geg/net/ghttp/server/template/layout/template/main/main2.html new file mode 100644 index 000000000..608512269 --- /dev/null +++ b/geg/net/ghttp/server/template/layout/template/main/main2.html @@ -0,0 +1 @@ +

MAIN2

\ No newline at end of file