mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 12:47:50 +08:00
add example for layout using template engine; fix issue with config error output in gview when no config used
This commit is contained in:
parent
86834c5a15
commit
fa256aec9f
@ -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("")
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
// 设置配置管理对象的默认文件名称
|
||||
|
@ -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
|
||||
|
23
geg/net/ghttp/server/template/layout/main.go
Normal file
23
geg/net/ghttp/server/template/layout/main.go
Normal 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()
|
||||
}
|
@ -0,0 +1 @@
|
||||
<h1>FOOTER</h1>
|
@ -0,0 +1 @@
|
||||
<h1>HEADER</h1>
|
@ -0,0 +1,3 @@
|
||||
{{include "header.html" .}}
|
||||
{{include .mainTpl .}}
|
||||
{{include "footer.html" .}}
|
@ -0,0 +1 @@
|
||||
<h1>MAIN1</h1>
|
@ -0,0 +1 @@
|
||||
<h1>MAIN2</h1>
|
Loading…
Reference in New Issue
Block a user