fix issue in template for ghttp.Server

This commit is contained in:
John 2019-10-17 20:31:03 +08:00
parent 97fe8235da
commit 500efb5601
14 changed files with 107 additions and 5 deletions

View File

@ -230,7 +230,7 @@ func New(name ...string) (db DB, err error) {
// which is DEFAULT_GROUP_NAME in default. // which is DEFAULT_GROUP_NAME in default.
func Instance(name ...string) (db DB, err error) { func Instance(name ...string) (db DB, err error) {
group := configs.defaultGroup group := configs.defaultGroup
if len(name) > 0 { if len(name) > 0 && name[0] != "" {
group = name[0] group = name[0]
} }
v := instances.GetOrSetFuncLock(group, func() interface{} { v := instances.GetOrSetFuncLock(group, func() interface{} {

View File

@ -118,7 +118,7 @@ func (m *Manager) Translate(content string, language ...string) string {
m.mu.RLock() m.mu.RLock()
defer m.mu.RUnlock() defer m.mu.RUnlock()
var data map[string]string var data map[string]string
if len(language) > 0 { if len(language) > 0 && language[0] != "" {
data = m.data[language[0]] data = m.data[language[0]]
} else { } else {
data = m.data[m.options.Language] data = m.data[m.options.Language]

View File

@ -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{} { func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
vars := map[string]interface{}(nil) vars := map[string]interface{}(nil)
if len(params) > 0 { if len(params) > 0 && params[0] != nil {
vars = params[0] vars = params[0]
} else { } else {
vars = make(map[string]interface{}) vars = make(map[string]interface{})

View File

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

View File

@ -0,0 +1 @@
{{define "container"}}2{{end}}

View File

@ -0,0 +1 @@
{{define "footer"}}3{{end}}

View File

@ -0,0 +1 @@
{{define "header"}}1{{end}}

View File

@ -0,0 +1 @@
{{template "header"}}{{template "container"}}{{template "footer"}}

View File

@ -0,0 +1 @@
b

View File

@ -0,0 +1 @@
a

View File

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

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
2

View File

@ -74,13 +74,17 @@ func (view *View) funcGe(value interface{}, other interface{}) bool {
} }
// Build-in template function: include // 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 var m map[string]interface{} = nil
if len(data) > 0 { if len(data) > 0 {
m = data[0] m = data[0]
} }
path := gconv.String(file)
if path == "" {
return ""
}
// It will search the file internally. // It will search the file internally.
content, err := view.Parse(file, m) content, err := view.Parse(path, m)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }