mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
fix issue in StrLimit for gstr
This commit is contained in:
16
.example/os/gview/build_in_funcs/issue359-1.go
Normal file
16
.example/os/gview/build_in_funcs/issue359-1.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tplContent := `
|
||||
{{"我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人"| strlimit 10 "..."}}
|
||||
`
|
||||
content, err := g.View().ParseContent(tplContent, nil)
|
||||
fmt.Println(err)
|
||||
fmt.Println(content)
|
||||
}
|
||||
19
.example/os/gview/build_in_funcs/issue359-2.go
Normal file
19
.example/os/gview/build_in_funcs/issue359-2.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := "我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人"
|
||||
tplContent := `
|
||||
{{.str | strlimit 10 "..."}}
|
||||
`
|
||||
content, err := g.View().ParseContent(tplContent, g.Map{
|
||||
"str": s,
|
||||
})
|
||||
fmt.Println(err)
|
||||
fmt.Println(content)
|
||||
}
|
||||
@ -186,7 +186,7 @@ func SubStr(str string, start int, length ...int) (substr string) {
|
||||
// then the <suffix> will be appended to the result string.
|
||||
func StrLimit(str string, length int, suffix ...string) string {
|
||||
rs := []rune(str)
|
||||
if len(str) < length {
|
||||
if len(rs) < length {
|
||||
return str
|
||||
}
|
||||
addStr := "..."
|
||||
|
||||
Reference in New Issue
Block a user