Skip to content

Commit 7706c73

Browse files
authored
Merge pull request #3 from infosiftr/godoc
Add some godocs for templatelib (mostly so I don't forget again how to use it, but also for others)
2 parents 5ab0344 + 2406f9a commit 7706c73

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pkg/templatelib/doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Package templatelib implements a group of useful functions for use with the stdlib text/template package.
3+
4+
Usage:
5+
6+
tmpl, err := template.New("some-template").Funcs(templatelib.FuncMap).Parse("Hi, {{ join " " .Names }}")
7+
*/
8+
package templatelib

pkg/templatelib/lib.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ func stringsModifierActionFactory(a func(string, string) string) func([]string,
7070
// TODO write some tests for these
7171

7272
var FuncMap = template.FuncMap{
73+
// {{- $isGitHub := hasPrefix "https://github.com/" $url -}}
74+
// {{- $isHtml := hasSuffix ".html" $url -}}
7375
"hasPrefix": swapStringsFuncBoolArgsOrder(strings.HasPrefix),
7476
"hasSuffix": swapStringsFuncBoolArgsOrder(strings.HasSuffix),
7577

78+
// {{- $hugeIfTrue := .SomeValue | ternary "HUGE" "not so huge" -}}
79+
// if .SomeValue is truthy, $hugeIfTrue will be "HUGE"
80+
// (otherwise, "not so huge")
7681
"ternary": func(truthy interface{}, falsey interface{}, val interface{}) interface{} {
7782
if t, ok := template.IsTrue(val); !ok {
7883
panic(fmt.Sprintf(`template.IsTrue(%+v) says things are NOT OK`, val))
@@ -83,14 +88,26 @@ var FuncMap = template.FuncMap{
8388
}
8489
},
8590

91+
// First Tag: {{- .Tags | first -}}
92+
// Last Tag: {{- .Tags | last -}}
8693
"first": thingsActionFactory("first", true, func(args []interface{}, arg interface{}) interface{} { return arg }),
8794
"last": thingsActionFactory("last", false, func(args []interface{}, arg interface{}) interface{} { return arg }),
8895

96+
// JSON data dump: {{ json . }}
97+
// (especially nice for taking data and piping it to "jq")
98+
// (ie "some-tool inspect --format '{{ json . }}' some-things | jq .")
8999
"json": func(v interface{}) (string, error) {
90100
j, err := json.Marshal(v)
91101
return string(j), err
92102
},
93-
"join": stringsActionFactory("join", true, strings.Join),
103+
104+
// Everybody: {{- join ", " .Names -}}
105+
// Concat: {{- join "/" "https://github.com" "jsmith" "some-repo" -}}
106+
"join": stringsActionFactory("join", true, strings.Join),
107+
108+
// {{- $mungedUrl := $url | replace "git://" "https://" | trimSuffixes ".git" -}}
109+
// turns: git://github.com/jsmith/some-repo.git
110+
// into: https://github.com/jsmith/some-repo
94111
"trimPrefixes": stringsActionFactory("trimPrefixes", false, stringsModifierActionFactory(strings.TrimPrefix)),
95112
"trimSuffixes": stringsActionFactory("trimSuffixes", false, stringsModifierActionFactory(strings.TrimSuffix)),
96113
"replace": stringsActionFactory("replace", false, func(strs []string, str string) string {

0 commit comments

Comments
 (0)