Make TrimSpace available for use in the template (#3281)

* Make TrimSpace available for use in the template

Signed-off-by: Ghazanfar <ghazanfar.isc@gmail.com>
This commit is contained in:
Ghazanfar 2023-03-07 14:08:57 +05:30 committed by GitHub
parent 41eb1213bb
commit d8bea84adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -89,6 +89,7 @@ templating.
| title | string |[strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. |
| toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. |
| toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. |
| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. |
| match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. |
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |

View File

@ -168,9 +168,11 @@ func (t *Template) ExecuteHTMLString(html string, data interface{}) (string, err
type FuncMap map[string]interface{}
var DefaultFuncs = FuncMap{
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"title": cases.Title(language.AmericanEnglish).String,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"trimSpace": strings.TrimSpace,
"title": cases.Title(language.AmericanEnglish).String,
// join is equal to strings.Join but inverts the argument order
// for easier pipelining in templates.
"join": func(sep string, s []string) string {

View File

@ -328,6 +328,11 @@ func TestTemplateExpansion(t *testing.T) {
in: `{{ "abc" | title }}`,
exp: "Abc",
},
{
title: "Template using TrimSpace",
in: `{{ " a b c " | trimSpace }}`,
exp: "a b c",
},
{
title: "Template using positive match",
in: `{{ if match "^a" "abc"}}abc{{ end }}`,