github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/pkg/templates/templates.go (about) 1 package templates 2 3 import ( 4 "encoding/json" 5 "strings" 6 "text/template" 7 ) 8 9 // basicFunctions are the set of initial 10 // functions provided to every template. 11 var basicFunctions = template.FuncMap{ 12 "json": func(v interface{}) string { 13 a, _ := json.Marshal(v) 14 return string(a) 15 }, 16 "split": strings.Split, 17 "join": strings.Join, 18 "title": strings.Title, 19 "lower": strings.ToLower, 20 "upper": strings.ToUpper, 21 "pad": padWithSpace, 22 } 23 24 // Parse creates a new annonymous template with the basic functions 25 // and parses the given format. 26 func Parse(format string) (*template.Template, error) { 27 return NewParse("", format) 28 } 29 30 // NewParse creates a new tagged template with the basic functions 31 // and parses the given format. 32 func NewParse(tag, format string) (*template.Template, error) { 33 return template.New(tag).Funcs(basicFunctions).Parse(format) 34 } 35 36 // padWithSpace adds whitespace to the input if the input is non-empty 37 func padWithSpace(source string, prefix, suffix int) string { 38 if source == "" { 39 return source 40 } 41 return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix) 42 }