mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +08:00
improve autoencode feature for package gview
This commit is contained in:
parent
c0a0913d4b
commit
16958413bb
@ -12,6 +12,12 @@ import (
|
||||
|
||||
// TestDataPath retrieves and returns the testdata path of current package.
|
||||
// It is used for unit testing cases only.
|
||||
func TestDataPath() string {
|
||||
return CallerDirectory() + string(filepath.Separator) + "testdata"
|
||||
// The parameter <names> specifies the underlying sub-folders/sub-files,
|
||||
// which will be joined with current system separator.
|
||||
func TestDataPath(names ...string) string {
|
||||
path := CallerDirectory() + string(filepath.Separator) + "testdata"
|
||||
for _, name := range names {
|
||||
path += string(filepath.Separator) + name
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
38
encoding/gjson/gjson_z_example_load_test.go
Normal file
38
encoding/gjson/gjson_z_example_load_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
package gjson_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func Example_LoadJson() {
|
||||
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "json", "data1.json")
|
||||
j, _ := gjson.Load(jsonFilePath)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
}
|
||||
|
||||
func Example_LoadXml() {
|
||||
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "xml", "data1.xml")
|
||||
j, _ := gjson.Load(jsonFilePath)
|
||||
fmt.Println(j.Get("doc.name"))
|
||||
fmt.Println(j.Get("doc.score"))
|
||||
}
|
||||
|
||||
func Example_LoadContent() {
|
||||
jsonContent := `{"name":"john", "score":"100"}`
|
||||
j, _ := gjson.LoadContent(jsonContent)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
70
encoding/gjson/gjson_z_example_new_test.go
Normal file
70
encoding/gjson/gjson_z_example_new_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
package gjson_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
func Example_NewFromJson() {
|
||||
jsonContent := `{"name":"john", "score":"100"}`
|
||||
j := gjson.New(jsonContent)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromXml() {
|
||||
jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
|
||||
j := gjson.New(jsonContent)
|
||||
fmt.Println(j.Get("doc.name"))
|
||||
fmt.Println(j.Get("doc.score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromStruct() {
|
||||
type Me struct {
|
||||
Name string `json:"name"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
me := Me{
|
||||
Name: "john",
|
||||
Score: 100,
|
||||
}
|
||||
j := gjson.New(me)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromStructWithTag() {
|
||||
type Me struct {
|
||||
Name string `tag:"name"`
|
||||
Score int `tag:"score"`
|
||||
Title string
|
||||
}
|
||||
me := Me{
|
||||
Name: "john",
|
||||
Score: 100,
|
||||
Title: "engineer",
|
||||
}
|
||||
j := gjson.NewWithTag(me, "tag")
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
fmt.Println(j.Get("Title"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
// engineer
|
||||
}
|
1
encoding/gjson/testdata/json/data1.json
vendored
Normal file
1
encoding/gjson/testdata/json/data1.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"name":"john", "score":"100"}
|
5
encoding/gjson/testdata/xml/data1.xml
vendored
Normal file
5
encoding/gjson/testdata/xml/data1.xml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<doc>
|
||||
<name>john</name>
|
||||
<score>100</score>
|
||||
</doc>
|
@ -47,6 +47,32 @@ func Test_Template_Basic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Template_Encode(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := gview.New(gfile.Join(gdebug.TestDataPath(), "template", "basic"))
|
||||
v.SetAutoEncode(true)
|
||||
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"))
|
||||
|
@ -142,7 +142,11 @@ func (view *View) Parse(file string, params ...Params) (result string, err error
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if view.config.AutoEncode {
|
||||
if err := tpl.(*htmltpl.Template).Execute(buffer, variables); err != nil {
|
||||
newTpl, err := tpl.(*htmltpl.Template).Clone()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := newTpl.Execute(buffer, variables); err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
@ -221,7 +225,11 @@ func (view *View) ParseContent(content string, params ...Params) (string, error)
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if view.config.AutoEncode {
|
||||
if err := tpl.(*htmltpl.Template).Execute(buffer, variables); err != nil {
|
||||
newTpl, err := tpl.(*htmltpl.Template).Clone()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := newTpl.Execute(buffer, variables); err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
42
os/gview/gview_unit_encode_test.go
Normal file
42
os/gview/gview_unit_encode_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2020 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.
|
||||
|
||||
package gview_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Encode_Parse(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := gview.New()
|
||||
v.SetPath(gdebug.TestDataPath("tpl"))
|
||||
v.SetAutoEncode(true)
|
||||
result, err := v.Parse("encode.tpl", g.Map{
|
||||
"title": "<b>my title</b>",
|
||||
})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(result, "<div><b>my title</b></div>")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Encode_ParseContent(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := gview.New()
|
||||
tplContent := gfile.GetContents(gdebug.TestDataPath("tpl", "encode.tpl"))
|
||||
v.SetAutoEncode(true)
|
||||
result, err := v.ParseContent(tplContent, g.Map{
|
||||
"title": "<b>my title</b>",
|
||||
})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(result, "<div><b>my title</b></div>")
|
||||
})
|
||||
}
|
1
os/gview/testdata/tpl/encode.tpl
vendored
Normal file
1
os/gview/testdata/tpl/encode.tpl
vendored
Normal file
@ -0,0 +1 @@
|
||||
<div>{{.title}}</div>
|
Loading…
Reference in New Issue
Block a user