add build-in function 'replace' for gview

This commit is contained in:
John
2019-11-07 20:56:17 +08:00
parent 9da1277b47
commit d7b0228e9e
3 changed files with 47 additions and 36 deletions

View File

@ -132,6 +132,7 @@ func New(path ...string) *View {
view.BindFunc("date", view.funcDate)
view.BindFunc("substr", view.funcSubStr)
view.BindFunc("strlimit", view.funcStrLimit)
view.BindFunc("replace", view.funcReplace)
view.BindFunc("compare", view.funcCompare)
view.BindFunc("hidestr", view.funcHideStr)
view.BindFunc("highlight", view.funcHighlight)

View File

@ -17,7 +17,7 @@ import (
"github.com/gogf/gf/util/gconv"
)
// Build-in template function: eq
// funcEq implements build-in template function: eq
func (view *View) funcEq(value interface{}, others ...interface{}) bool {
s := gconv.String(value)
for _, v := range others {
@ -28,13 +28,13 @@ func (view *View) funcEq(value interface{}, others ...interface{}) bool {
return true
}
// Build-in template function: ne
func (view *View) funcNe(value interface{}, other interface{}) bool {
// funcNe implements build-in template function: ne
func (view *View) funcNe(value, other interface{}) bool {
return strings.Compare(gconv.String(value), gconv.String(other)) != 0
}
// Build-in template function: lt
func (view *View) funcLt(value interface{}, other interface{}) bool {
// funcLt implements build-in template function: lt
func (view *View) funcLt(value, other interface{}) bool {
s1 := gconv.String(value)
s2 := gconv.String(other)
if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
@ -43,8 +43,8 @@ func (view *View) funcLt(value interface{}, other interface{}) bool {
return strings.Compare(s1, s2) < 0
}
// Build-in template function: le
func (view *View) funcLe(value interface{}, other interface{}) bool {
// funcLe implements build-in template function: le
func (view *View) funcLe(value, other interface{}) bool {
s1 := gconv.String(value)
s2 := gconv.String(other)
if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
@ -53,8 +53,8 @@ func (view *View) funcLe(value interface{}, other interface{}) bool {
return strings.Compare(s1, s2) <= 0
}
// Build-in template function: gt
func (view *View) funcGt(value interface{}, other interface{}) bool {
// funcGt implements build-in template function: gt
func (view *View) funcGt(value, other interface{}) bool {
s1 := gconv.String(value)
s2 := gconv.String(other)
if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
@ -63,8 +63,8 @@ func (view *View) funcGt(value interface{}, other interface{}) bool {
return strings.Compare(s1, s2) > 0
}
// Build-in template function: ge
func (view *View) funcGe(value interface{}, other interface{}) bool {
// funcGe implements build-in template function: ge
func (view *View) funcGe(value, other interface{}) bool {
s1 := gconv.String(value)
s2 := gconv.String(other)
if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
@ -73,7 +73,7 @@ func (view *View) funcGe(value interface{}, other interface{}) bool {
return strings.Compare(s1, s2) >= 0
}
// Build-in template function: include
// funcInclude implements build-in template function: include
func (view *View) funcInclude(file interface{}, data ...map[string]interface{}) string {
var m map[string]interface{} = nil
if len(data) > 0 {
@ -91,27 +91,27 @@ func (view *View) funcInclude(file interface{}, data ...map[string]interface{})
return content
}
// Build-in template function: text
// funcText implements build-in template function: text
func (view *View) funcText(html interface{}) string {
return ghtml.StripTags(gconv.String(html))
}
// Build-in template function: html
// funcHtmlEncode implements build-in template function: html
func (view *View) funcHtmlEncode(html interface{}) string {
return ghtml.Entities(gconv.String(html))
}
// Build-in template function: htmldecode
// funcHtmlDecode implements build-in template function: htmldecode
func (view *View) funcHtmlDecode(html interface{}) string {
return ghtml.EntitiesDecode(gconv.String(html))
}
// Build-in template function: url
// funcUrlEncode implements build-in template function: url
func (view *View) funcUrlEncode(url interface{}) string {
return gurl.Encode(gconv.String(url))
}
// Build-in template function: urldecode
// funcUrlDecode implements build-in template function: urldecode
func (view *View) funcUrlDecode(url interface{}) string {
if content, err := gurl.Decode(gconv.String(url)); err == nil {
return content
@ -120,8 +120,8 @@ func (view *View) funcUrlDecode(url interface{}) string {
}
}
// Build-in template function: date
func (view *View) funcDate(format string, timestamp ...interface{}) string {
// funcDate implements build-in template function: date
func (view *View) funcDate(format interface{}, timestamp ...interface{}) string {
t := int64(0)
if len(timestamp) > 0 {
t = gconv.Int64(timestamp[0])
@ -129,45 +129,50 @@ func (view *View) funcDate(format string, timestamp ...interface{}) string {
if t == 0 {
t = gtime.Millisecond()
}
return gtime.NewFromTimeStamp(t).Format(format)
return gtime.NewFromTimeStamp(t).Format(gconv.String(format))
}
// Build-in template function: compare
// funcCompare implements build-in template function: compare
func (view *View) funcCompare(value1, value2 interface{}) int {
return strings.Compare(gconv.String(value1), gconv.String(value2))
}
// Build-in template function: substr
func (view *View) funcSubStr(start, end int, str interface{}) string {
return gstr.SubStr(gconv.String(str), start, end)
// SubStr implements build-in template function: substr
func (view *View) funcSubStr(start, end, str interface{}) string {
return gstr.SubStr(gconv.String(str), gconv.Int(start), gconv.Int(end))
}
// Build-in template function: strlimit
func (view *View) funcStrLimit(length int, suffix string, str interface{}) string {
return gstr.StrLimit(gconv.String(str), length, suffix)
// StrLimit implements build-in template function: strlimit
func (view *View) funcStrLimit(length, suffix, str interface{}) string {
return gstr.StrLimit(gconv.String(str), gconv.Int(length), gconv.String(suffix))
}
// Build-in template function: highlight
func (view *View) funcHighlight(key string, color string, str interface{}) string {
return gstr.Replace(gconv.String(str), key, fmt.Sprintf(`<span style="color:%s;">%s</span>`, color, key))
// funcReplace implements build-in template function: replace
func (view *View) funcReplace(search, replace, str interface{}) string {
return gstr.Replace(gconv.String(str), gconv.String(search), gconv.String(replace), -1)
}
// Build-in template function: hidestr
func (view *View) funcHideStr(percent int, hide string, str interface{}) string {
return gstr.HideStr(gconv.String(str), percent, hide)
// funcHighlight implements build-in template function: highlight
func (view *View) funcHighlight(key, color, str interface{}) string {
return gstr.Replace(gconv.String(str), gconv.String(key), fmt.Sprintf(`<span style="color:%v;">%v</span>`, color, key))
}
// Build-in template function: toupper
// funcHideStr implements build-in template function: hidestr
func (view *View) funcHideStr(percent, hide, str interface{}) string {
return gstr.HideStr(gconv.String(str), gconv.Int(percent), gconv.String(hide))
}
// funcToUpper implements build-in template function: toupper
func (view *View) funcToUpper(str interface{}) string {
return gstr.ToUpper(gconv.String(str))
}
// Build-in template function: toupper
// funcToLower implements build-in template function: toupper
func (view *View) funcToLower(str interface{}) string {
return gstr.ToLower(gconv.String(str))
}
// Build-in template function: nl2br
// funcNl2Br implements build-in template function: nl2br
func (view *View) funcNl2Br(str interface{}) string {
return gstr.Nl2Br(gconv.String(str))
}

View File

@ -143,6 +143,11 @@ func Test_Func(t *testing.T) {
gtest.Assert(err != nil, false)
gtest.Assert(result, `我是...`)
str = `{{"I'm中国人" | replace "I'm" "我是"}}`
result, err = gview.ParseContent(str, nil)
gtest.Assert(err != nil, false)
gtest.Assert(result, `我是中国人`)
str = `{{compare "A" "B"}};{{compare "1" "2"}};{{compare 2 1}};{{compare 1 1}}`
result, err = gview.ParseContent(str, nil)
gtest.Assert(err != nil, false)