add example for layout using template engine; fix issue with config error output in gview when no config used

This commit is contained in:
John 2019-02-27 22:53:39 +08:00
parent 86834c5a15
commit fa256aec9f
9 changed files with 60 additions and 9 deletions

View File

@ -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("")

View File

@ -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
}
// 设置配置管理对象的默认文件名称

View File

@ -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

View File

@ -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()
}

View File

@ -0,0 +1 @@
<h1>FOOTER</h1>

View File

@ -0,0 +1 @@
<h1>HEADER</h1>

View File

@ -0,0 +1,3 @@
{{include "header.html" .}}
{{include .mainTpl .}}
{{include "footer.html" .}}

View File

@ -0,0 +1 @@
<h1>MAIN1</h1>

View File

@ -0,0 +1 @@
<h1>MAIN2</h1>