Skip to content

Commit

Permalink
Merge pull request #229 from derBiggi/master
Browse files Browse the repository at this point in the history
Expose ToInt conversion for usage in template
  • Loading branch information
Laurie Clark-Michalek committed Jan 16, 2017
2 parents 0cb8920 + 4895666 commit 1eede2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion services/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"bytes"
"github.com/QubitProducts/bamboo/services/service"
"strconv"
"strings"
"text/template"
)
Expand All @@ -29,7 +30,8 @@ func RenderTemplate(templateName string, templateContent string, data interface{
"Join": strings.Join,
"Replace": strings.Replace,
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower}
"ToLower": strings.ToLower,
"ToInt": strconv.Atoi}

tpl := template.Must(template.New(templateName).Funcs(funcMap).Parse(templateContent))

Expand Down
14 changes: 14 additions & 0 deletions services/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,17 @@ func TestTemplateToLowerFunction(t *testing.T) {
})
})
}

func TestTemplateToIntFunction(t *testing.T) {
Convey("#RenderTemplate", t, func() {
templateName := "templateName"
domains := []string{"example.com", "example.net"}
params := map[string]interface{}{"idx": "1", "domains": domains}

templateContent := "{{index .domains (ToInt .idx)}}"
Convey("should transform an indexo to integer", func() {
content, _ := RenderTemplate(templateName, templateContent, params)
So(content, ShouldEqual, "example.net")
})
})
}

0 comments on commit 1eede2a

Please sign in to comment.