Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit eabef25

Browse files
authored
Merge pull request #291 from rafaelpirolla/master
Add template replace function
2 parents 4384cd0 + 5f35830 commit eabef25

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

docs/templates.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ Render:
307307
100
308308
```
309309

310+
#### replace
311+
312+
Replaces all occurrences of the search string with the replacement string.
313+
314+
Example:
315+
```
316+
[[ replace "Batman and Robin" "Robin" "Catwoman" ]]
317+
```
318+
319+
Render:
320+
```
321+
Batman and Catwoman
322+
```
323+
310324
#### timeNow
311325

312326
Returns the current ISO_8601 standard timestamp as a string in the timezone of the machine the rendering was triggered on.

template/funcs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func funcMap(consulClient *consul.Client) template.FuncMap {
3131
"parseInt": parseInt,
3232
"parseJSON": parseJSON,
3333
"parseUint": parseUint,
34+
"replace": replace,
3435
"timeNow": timeNowFunc,
3536
"timeNowUTC": timeNowUTCFunc,
3637
"timeNowTimezone": timeNowTimezoneFunc(),
@@ -202,6 +203,10 @@ func parseUint(s string) (uint64, error) {
202203
return result, nil
203204
}
204205

206+
func replace(input, from, to string) string {
207+
return strings.Replace(input, from, to, -1)
208+
}
209+
205210
func timeNowFunc() string {
206211
return time.Now().Format("2006-01-02T15:04:05Z07:00")
207212
}

0 commit comments

Comments
 (0)