From 500efb56017f2ebf9013506f7c1444f2ab69bb0a Mon Sep 17 00:00:00 2001 From: John Date: Thu, 17 Oct 2019 20:31:03 +0800 Subject: [PATCH] fix issue in template for ghttp.Server --- database/gdb/gdb.go | 2 +- i18n/gi18n/gi18n_manager.go | 2 +- net/ghttp/ghttp_response_view.go | 2 +- net/ghttp/ghttp_unit_template_test.go | 89 +++++++++++++++++++ .../testdata/template/layout1/container.html | 1 + .../testdata/template/layout1/footer.html | 1 + .../testdata/template/layout1/header.html | 1 + .../testdata/template/layout1/layout.html | 1 + .../testdata/template/layout2/footer.html | 1 + .../testdata/template/layout2/header.html | 1 + .../testdata/template/layout2/layout.html | 1 + .../testdata/template/layout2/main/main1.html | 1 + .../testdata/template/layout2/main/main2.html | 1 + os/gview/gview_buildin.go | 8 +- 14 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 net/ghttp/ghttp_unit_template_test.go create mode 100644 net/ghttp/testdata/template/layout1/container.html create mode 100644 net/ghttp/testdata/template/layout1/footer.html create mode 100644 net/ghttp/testdata/template/layout1/header.html create mode 100644 net/ghttp/testdata/template/layout1/layout.html create mode 100644 net/ghttp/testdata/template/layout2/footer.html create mode 100644 net/ghttp/testdata/template/layout2/header.html create mode 100644 net/ghttp/testdata/template/layout2/layout.html create mode 100644 net/ghttp/testdata/template/layout2/main/main1.html create mode 100644 net/ghttp/testdata/template/layout2/main/main2.html diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 1d6a415c0..5b8da1c25 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -230,7 +230,7 @@ func New(name ...string) (db DB, err error) { // which is DEFAULT_GROUP_NAME in default. func Instance(name ...string) (db DB, err error) { group := configs.defaultGroup - if len(name) > 0 { + if len(name) > 0 && name[0] != "" { group = name[0] } v := instances.GetOrSetFuncLock(group, func() interface{} { diff --git a/i18n/gi18n/gi18n_manager.go b/i18n/gi18n/gi18n_manager.go index 4a2a4f1a2..d19aaaf76 100644 --- a/i18n/gi18n/gi18n_manager.go +++ b/i18n/gi18n/gi18n_manager.go @@ -118,7 +118,7 @@ func (m *Manager) Translate(content string, language ...string) string { m.mu.RLock() defer m.mu.RUnlock() var data map[string]string - if len(language) > 0 { + if len(language) > 0 && language[0] != "" { data = m.data[language[0]] } else { data = m.data[m.options.Language] diff --git a/net/ghttp/ghttp_response_view.go b/net/ghttp/ghttp_response_view.go index 2be14cc02..77d6c3bf1 100644 --- a/net/ghttp/ghttp_response_view.go +++ b/net/ghttp/ghttp_response_view.go @@ -58,7 +58,7 @@ func (r *Response) ParseTplContent(content string, params ...gview.Params) (stri // 内置变量/对象 func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} { vars := map[string]interface{}(nil) - if len(params) > 0 { + if len(params) > 0 && params[0] != nil { vars = params[0] } else { vars = make(map[string]interface{}) diff --git a/net/ghttp/ghttp_unit_template_test.go b/net/ghttp/ghttp_unit_template_test.go new file mode 100644 index 000000000..efa9061e8 --- /dev/null +++ b/net/ghttp/ghttp_unit_template_test.go @@ -0,0 +1,89 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// static service testing. + +package ghttp_test + +import ( + "fmt" + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gview" + "testing" + "time" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/gfile" + "github.com/gogf/gf/test/gtest" +) + +func Test_Template_Layout1(t *testing.T) { + gtest.Case(t, func() { + v := gview.New(gfile.Join(gdebug.CallerDirectory(), "testdata", "template", "layout1")) + p := ports.PopRand() + s := g.Server(p) + s.SetView(v) + s.BindHandler("/layout", func(r *ghttp.Request) { + err := r.Response.WriteTpl("layout.html", g.Map{ + "mainTpl": "main/main1.html", + }) + gtest.Assert(err, nil) + }) + s.BindHandler("/nil", func(r *ghttp.Request) { + err := r.Response.WriteTpl("layout.html", nil) + gtest.Assert(err, nil) + }) + s.SetDumpRouteMap(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)) + + gtest.Assert(client.GetContent("/"), "Not Found") + gtest.Assert(client.GetContent("/layout"), "123") + gtest.Assert(client.GetContent("/nil"), "123") + }) +} + +func Test_Template_Layout2(t *testing.T) { + gtest.Case(t, func() { + v := gview.New(gfile.Join(gdebug.CallerDirectory(), "testdata", "template", "layout2")) + p := ports.PopRand() + s := g.Server(p) + s.SetView(v) + s.BindHandler("/main1", func(r *ghttp.Request) { + err := r.Response.WriteTpl("layout.html", g.Map{ + "mainTpl": "main/main1.html", + }) + gtest.Assert(err, nil) + }) + s.BindHandler("/main2", func(r *ghttp.Request) { + err := r.Response.WriteTpl("layout.html", g.Map{ + "mainTpl": "main/main2.html", + }) + gtest.Assert(err, nil) + }) + s.BindHandler("/nil", func(r *ghttp.Request) { + err := r.Response.WriteTpl("layout.html", nil) + gtest.Assert(err, nil) + }) + s.SetDumpRouteMap(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)) + + gtest.Assert(client.GetContent("/"), "Not Found") + gtest.Assert(client.GetContent("/main1"), "a1b") + gtest.Assert(client.GetContent("/main2"), "a2b") + gtest.Assert(client.GetContent("/nil"), "ab") + }) +} diff --git a/net/ghttp/testdata/template/layout1/container.html b/net/ghttp/testdata/template/layout1/container.html new file mode 100644 index 000000000..e5eb81673 --- /dev/null +++ b/net/ghttp/testdata/template/layout1/container.html @@ -0,0 +1 @@ +{{define "container"}}2{{end}} \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout1/footer.html b/net/ghttp/testdata/template/layout1/footer.html new file mode 100644 index 000000000..eb11b69ed --- /dev/null +++ b/net/ghttp/testdata/template/layout1/footer.html @@ -0,0 +1 @@ +{{define "footer"}}3{{end}} \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout1/header.html b/net/ghttp/testdata/template/layout1/header.html new file mode 100644 index 000000000..4870bee07 --- /dev/null +++ b/net/ghttp/testdata/template/layout1/header.html @@ -0,0 +1 @@ +{{define "header"}}1{{end}} \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout1/layout.html b/net/ghttp/testdata/template/layout1/layout.html new file mode 100644 index 000000000..9013318cc --- /dev/null +++ b/net/ghttp/testdata/template/layout1/layout.html @@ -0,0 +1 @@ +{{template "header"}}{{template "container"}}{{template "footer"}} \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout2/footer.html b/net/ghttp/testdata/template/layout2/footer.html new file mode 100644 index 000000000..63d8dbd40 --- /dev/null +++ b/net/ghttp/testdata/template/layout2/footer.html @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout2/header.html b/net/ghttp/testdata/template/layout2/header.html new file mode 100644 index 000000000..2e65efe2a --- /dev/null +++ b/net/ghttp/testdata/template/layout2/header.html @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout2/layout.html b/net/ghttp/testdata/template/layout2/layout.html new file mode 100644 index 000000000..c72bbc70e --- /dev/null +++ b/net/ghttp/testdata/template/layout2/layout.html @@ -0,0 +1 @@ +{{include "header.html" .}}{{include .mainTpl .}}{{include "footer.html" .}} \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout2/main/main1.html b/net/ghttp/testdata/template/layout2/main/main1.html new file mode 100644 index 000000000..56a6051ca --- /dev/null +++ b/net/ghttp/testdata/template/layout2/main/main1.html @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/net/ghttp/testdata/template/layout2/main/main2.html b/net/ghttp/testdata/template/layout2/main/main2.html new file mode 100644 index 000000000..d8263ee98 --- /dev/null +++ b/net/ghttp/testdata/template/layout2/main/main2.html @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/os/gview/gview_buildin.go b/os/gview/gview_buildin.go index 67ec72380..25d08171c 100644 --- a/os/gview/gview_buildin.go +++ b/os/gview/gview_buildin.go @@ -74,13 +74,17 @@ func (view *View) funcGe(value interface{}, other interface{}) bool { } // Build-in template function: include -func (view *View) funcInclude(file string, data ...map[string]interface{}) string { +func (view *View) funcInclude(file interface{}, data ...map[string]interface{}) string { var m map[string]interface{} = nil if len(data) > 0 { m = data[0] } + path := gconv.String(file) + if path == "" { + return "" + } // It will search the file internally. - content, err := view.Parse(file, m) + content, err := view.Parse(path, m) if err != nil { return err.Error() }