Skip to content

Commit

Permalink
template - minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Jan 9, 2024
1 parent 24c0ad7 commit fc8b5e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func templateFuncMap(r *rand.Rand, fm *template.FuncMap) *template.FuncMap {
return args
}

// Fix merge the user function maps with the template function map
// Add passed in function map to the function map
if fm != nil {
for k, v := range *fm {
funcMap[k] = v
Expand Down
13 changes: 11 additions & 2 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ func ExampleFaker_Template() {
// Sylvan Mraz
}

func TestUserFunctionMap(t *testing.T) {
func TestPassedInFunctionMap(t *testing.T) {
f := New(11)

funcMap := template.FuncMap{
"title": strings.Title,
"title": func(s string) string {
words := strings.Fields(s)
for i, word := range words {
letters := strings.Split(word, "")
letters[0] = strings.ToUpper(letters[0])
words[i] = strings.Join(letters, "")
}
return strings.Join(words, " ")
},
}

s := `{{printf "%q" (title .Data)}}`

value, err := f.Template(s, &TemplateOptions{Funcs: funcMap, Data: "the go programming language"})
Expand Down

0 comments on commit fc8b5e9

Please sign in to comment.