mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
fix issue in template for ghttp.Server
This commit is contained in:
parent
97fe8235da
commit
500efb5601
@ -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{} {
|
||||||
|
@ -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]
|
||||||
|
@ -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{})
|
||||||
|
89
net/ghttp/ghttp_unit_template_test.go
Normal file
89
net/ghttp/ghttp_unit_template_test.go
Normal 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")
|
||||||
|
})
|
||||||
|
}
|
1
net/ghttp/testdata/template/layout1/container.html
vendored
Normal file
1
net/ghttp/testdata/template/layout1/container.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{define "container"}}2{{end}}
|
1
net/ghttp/testdata/template/layout1/footer.html
vendored
Normal file
1
net/ghttp/testdata/template/layout1/footer.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{define "footer"}}3{{end}}
|
1
net/ghttp/testdata/template/layout1/header.html
vendored
Normal file
1
net/ghttp/testdata/template/layout1/header.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{define "header"}}1{{end}}
|
1
net/ghttp/testdata/template/layout1/layout.html
vendored
Normal file
1
net/ghttp/testdata/template/layout1/layout.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{template "header"}}{{template "container"}}{{template "footer"}}
|
1
net/ghttp/testdata/template/layout2/footer.html
vendored
Normal file
1
net/ghttp/testdata/template/layout2/footer.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
b
|
1
net/ghttp/testdata/template/layout2/header.html
vendored
Normal file
1
net/ghttp/testdata/template/layout2/header.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
a
|
1
net/ghttp/testdata/template/layout2/layout.html
vendored
Normal file
1
net/ghttp/testdata/template/layout2/layout.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{{include "header.html" .}}{{include .mainTpl .}}{{include "footer.html" .}}
|
1
net/ghttp/testdata/template/layout2/main/main1.html
vendored
Normal file
1
net/ghttp/testdata/template/layout2/main/main1.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
1
|
1
net/ghttp/testdata/template/layout2/main/main2.html
vendored
Normal file
1
net/ghttp/testdata/template/layout2/main/main2.html
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
2
|
@ -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()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user