diff --git a/.example/net/ghttp/server/template/tpl1/tpl1.go b/.example/net/ghttp/server/template/tpl1/tpl1.go index b75da20c4..d10af71b9 100644 --- a/.example/net/ghttp/server/template/tpl1/tpl1.go +++ b/.example/net/ghttp/server/template/tpl1/tpl1.go @@ -8,7 +8,6 @@ import ( func main() { s := ghttp.GetServer() s.BindHandler("/", func(r *ghttp.Request) { - r.Response.Write("Hello World") r.Response.WriteTpl("index.tpl", g.Map{ "title": "Test", "name": "John", diff --git a/net/ghttp/ghttp_unit_template_test.go b/net/ghttp/ghttp_unit_template_test.go index 619a7c37d..c292c6c04 100644 --- a/net/ghttp/ghttp_unit_template_test.go +++ b/net/ghttp/ghttp_unit_template_test.go @@ -22,6 +22,31 @@ import ( "github.com/gogf/gf/test/gtest" ) +func Test_Template_Basic(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "basic")) + p := ports.PopRand() + s := g.Server(p) + s.SetView(v) + s.BindHandler("/", func(r *ghttp.Request) { + err := r.Response.WriteTpl("index.html", g.Map{ + "name": "john", + }) + t.Assert(err, nil) + }) + s.SetDumpRouterMap(false) + s.SetPort(p) + s.Start() + defer s.Shutdown() + time.Sleep(100 * time.Millisecond) + client := ghttp.NewClient() + client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) + + t.Assert(client.GetContent("/"), "Name:john") + t.Assert(client.GetContent("/"), "Name:john") + }) +} + func Test_Template_Layout1(t *testing.T) { gtest.C(t, func(t *gtest.T) { v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "layout1")) diff --git a/net/ghttp/testdata/template/basic/index.html b/net/ghttp/testdata/template/basic/index.html new file mode 100644 index 000000000..6765dd9bc --- /dev/null +++ b/net/ghttp/testdata/template/basic/index.html @@ -0,0 +1 @@ +Name:{{.name}} \ No newline at end of file diff --git a/os/gsession/gsession_storage_file.go b/os/gsession/gsession_storage_file.go index fd6f18ec8..a5063abd5 100644 --- a/os/gsession/gsession_storage_file.go +++ b/os/gsession/gsession_storage_file.go @@ -160,7 +160,7 @@ func (s *StorageFile) GetSession(id string, ttl time.Duration, data *gmap.StrAny if data != nil { return data, nil } - intlog.Printf("StorageFile.GetSession: %s, %v", id, ttl) + //intlog.Printf("StorageFile.GetSession: %s, %v", id, ttl) path := s.sessionFilePath(id) content := gfile.GetBytes(path) if len(content) > 8 { diff --git a/os/gview/gview_doparse.go b/os/gview/gview_doparse.go index c5dc25f91..5fc05b70f 100644 --- a/os/gview/gview_doparse.go +++ b/os/gview/gview_doparse.go @@ -46,6 +46,7 @@ var ( // Templates cache map for template folder. // Note that there's no expiring logic for this map. templates = gmap.NewStrAnyMap(true) + // Try-folders for resource template file searching. resourceTryFolders = []string{"template/", "template", "/template", "/template/"} ) @@ -106,7 +107,9 @@ func (view *View) Parse(file string, params ...Params) (result string, err error tpl, err = tpl.(*texttpl.Template).Parse(item.content) } }) - + if err != nil { + return "", err + } // Note that the template variable assignment cannot change the value // of the existing or view.data because both variables are pointers. // It needs to merge the values of the two maps into a new map.