From 474e4857bd79fefb45baccc10803c0ebdb541700 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 1 May 2017 21:01:55 -0400 Subject: Add splitN function (i.e. strings.SplitN) Signed-off-by: Dave Henderson --- README.md | 24 ++++++++++++++++-------- gomplate.go | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 027e2525..20ffa21e 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ Gomplate is an alternative that will let you process templates which also includ - [Example](#example) - [`split`](#split) - [Example](#example) + - [`splitN`](#splitn) + - [Example](#example) - [`title`](#title) - [Example](#example) - [`toLower`](#tolower) @@ -361,20 +363,26 @@ Creates a slice by splitting a string on a given delimiter. Equivalent to ##### Example -_`input.tmpl`:_ -``` -{{range split "Bart,Lisa,Maggie"}} -Hello, {{.}} -{{- end}} -``` - ```console -$ gomplate < input.tmpl +$ gomplate -i '{{range split "Bart,Lisa,Maggie" ","}}Hello, {{.}}{{end}}' Hello, Bart Hello, Lisa Hello, Maggie ``` +#### `splitN` + +Creates a slice by splitting a string on a given delimiter. The count determines +the number of substrings to return. Equivalent to [strings.SplitN](https://golang.org/pkg/strings#SplitN) + +##### Example + +```console +$ gomplate -i '{{ range splitN "foo:bar:baz" ":" 2 }}{{.}}{{end}}' +foo +bar:baz +``` + #### `title` Convert to title-case. Equivalent to [strings.Title](https://golang.org/pkg/strings/#Title) diff --git a/gomplate.go b/gomplate.go index d3755809..1f9bca24 100644 --- a/gomplate.go +++ b/gomplate.go @@ -63,6 +63,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate { "hasPrefix": strings.HasPrefix, "hasSuffix": strings.HasSuffix, "split": strings.Split, + "splitN": strings.SplitN, "title": strings.Title, "toUpper": strings.ToUpper, "toLower": strings.ToLower, -- cgit v1.2.3