Skip to content

Commit 19e315f

Browse files
committed
add replace function
1 parent 87532f8 commit 19e315f

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ Usage of tto:
9999
```
100100
{{equal (3%2) 1}
101101
```
102+
替换, 如将`table_name`替换为:`table-name`
103+
```
104+
{{replace "table_name" "_" "-"}}
105+
```
102106
包含函数
103107
```
104108
{{contain .table.Pk "id"}}

const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package tto
22

33
// 版本号
4-
const BuildVersion = "0.3.14"
4+
const BuildVersion = "0.3.15"

template_func.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func (t *internalFunc) funcMap() ht.FuncMap {
3737
fm["default"] = t.langDefaultValue
3838
// 是否相等,如:{{equal "go" "rust"}
3939
fm["equal"] = t.equal
40+
// 替换,如: {{replace "table_name" "_" "-"}}
41+
fm["replace"] = t.replace
4042
// 包含函数, 如:{{contain .table.Pk "id"}}
4143
fm["contain"] = t.contain
4244
// 是否以指定字符开始, 如:{{starts_with .table.Pk "id"}}
@@ -116,6 +118,11 @@ func (t *internalFunc) equal(v1, v2 interface{}) bool {
116118
return v1 == v2
117119
}
118120

121+
// 替换,如: {{replace "table_name" "_" "-"}}
122+
func (t *internalFunc) replace(s,oldStr,newStr string) string {
123+
return strings.Replace(s,oldStr,newStr,-1)
124+
}
125+
119126
// 是否包含
120127
func (t *internalFunc) contain(v interface{}, s string) bool {
121128
if v == nil {

0 commit comments

Comments
 (0)