feat: add urlEncode and urlDecode functions to template rendering

This commit is contained in:
rick 2025-02-27 09:44:23 +00:00
parent 09fbe6dae8
commit 255579bd23
2 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
"fmt" "fmt"
"io" "io"
mathrand "math/rand" mathrand "math/rand"
"net/url"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@ -224,6 +225,17 @@ var advancedFuncs = []AdvancedFunc{{
Func: func() float64 { Func: func() float64 {
return time.Since(uptime).Seconds() return time.Since(uptime).Seconds()
}, },
}, {
FuncName: "urlEncode",
Func: func(text string) string {
return url.QueryEscape(text)
},
}, {
FuncName: "urlDecode",
Func: func(text string) (result string) {
result, _ = url.QueryUnescape(text)
return
},
}} }}
// WeightEnum is a weight enum // WeightEnum is a weight enum

View File

@ -127,6 +127,18 @@ func TestRender(t *testing.T) {
verify: func(t *testing.T, s string) { verify: func(t *testing.T, s string) {
assert.NotEmpty(t, s) assert.NotEmpty(t, s)
}, },
}, {
name: "url encode",
text: `{{urlEncode "hello world"}}`,
verify: func(t *testing.T, s string) {
assert.Equal(t, "hello+world", s)
},
}, {
name: "url decode",
text: `{{urlDecode "hello+world"}}`,
verify: func(t *testing.T, s string) {
assert.Equal(t, "hello world", s)
},
}} }}
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {