1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
---
title: strings functions
menu:
main:
parent: functions
---
## `strings.Abbrev`
Abbreviates a string using `...` (ellipses). Takes an optional offset from the beginning of the string, and a maximum final width (including added ellipses).
_Also see [`strings.Trunc`](#strings-trunc)._
### Usage
```go
strings.Abbrev [offset] width input
```
```go
input | strings.Abbrev [offset] width
```
### Arguments
| name | description |
|--------|-------|
| `offset` | _(optional)_ offset from the start of the string. Must be `4` or greater for ellipses to be added. Defaults to `0` |
| `width` | _(required)_ the desired maximum final width of the string, including ellipses |
| `input` | _(required)_ the input string to abbreviate |
### Example
```console
$ gomplate -i '{{ "foobarbazquxquux" | strings.Abbrev 9 }}'
foobar...
$ gomplate -i '{{ "foobarbazquxquux" | strings.Abbrev 6 9 }}'
...baz...
```
## `strings.Contains`
Reports whether a substring is contained within a string.
### Usage
```go
strings.Contains substr input
```
```go
input | strings.Contains substr
```
### Example
_`input.tmpl`:_
```
{{ if (.Env.FOO | strings.Contains "f") }}yes{{else}}no{{end}}
```
```console
$ FOO=foo gomplate < input.tmpl
yes
$ FOO=bar gomplate < input.tmpl
no
```
## `strings.HasPrefix`
Tests whether a string begins with a certain prefix.
### Usage
```go
strings.HasPrefix prefix input
```
```go
input | strings.HasPrefix prefix
```
#### Example
```console
$ URL=http://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
bar
$ URL=https://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
foo
```
## `strings.HasSuffix`
Tests whether a string ends with a certain suffix.
### Usage
```go
strings.HasSuffix suffix input
```
```go
input | strings.HasSuffix suffix
```
### Examples
_`input.tmpl`:_
```
{{.Env.URL}}{{if not (.Env.URL | strings.HasSuffix ":80")}}:80{{end}}
```
```console
$ URL=http://example.com gomplate < input.tmpl
http://example.com:80
```
## `strings.Indent`
**Alias:** `indent`
Indents a string. If the input string has multiple lines, each line will be indented.
### Usage
```go
strings.Indent [width] [indent] input
```
```go
input | strings.Indent [width] [indent]
```
### Arguments
| name | description |
|--------|-------|
| `width` | _(optional)_ number of times to repeat the `indent` string. Default: `1` |
| `indent` | _(optional)_ the string to indent with. Default: `" "` |
| `input` | the string to indent |
### Example
This function can be especially useful when adding YAML snippets into other YAML documents, where indentation is important:
_`input.tmpl`:_
```
foo:
{{ `{"bar": {"baz": 2}}` | json | toYAML | strings.Indent " " }}
{{- `{"qux": true}` | json | toYAML | strings.Indent 2 }}
quux:
{{ `{"quuz": 42}` | json | toYAML | strings.Indent 2 " " -}}
```
```console
$ gomplate -f input.tmpl
foo:
bar:
baz: 2
qux: true
quux:
quuz: 42
```
## `strings.Sort`
**Alias:** `sort`
Returns an alphanumerically-sorted copy of a given string list.
### Usage
```go
strings.Sort list
```
```go
list | strings.Sort
```
### Arguments
| name | description |
|------|-------------|
| `list` | _(required)_ The list to sort |
### Examples
```console
$ gomplate -i '{{ (slice "foo" "bar" "baz") | sort }}'
[bar baz foo]
```
## `strings.Split`
Creates a slice by splitting a string on a given delimiter.
### Usage
```go
strings.Split separator input
```
```go
input | strings.Split separator
```
### Examples
```console
$ gomplate -i '{{range ("Bart,Lisa,Maggie" | strings.Split ",") }}Hello, {{.}}{{end}}'
Hello, Bart
Hello, Lisa
Hello, Maggie
```
## `strings.SplitN`
Creates a slice by splitting a string on a given delimiter. The count determines
the number of substrings to return.
### Usage
```go
strings.SplitN separator count input
```
```go
input | strings.SplitN separator count
```
#### Example
```console
$ gomplate -i '{{ range ("foo:bar:baz" | strings.SplitN ":" 2) }}{{.}}{{end}}'
foo
bar:baz
```
## `strings.Repeat`
Returns a new string consisting of `count` copies of the input string.
It errors if `count` is negative or if the length of `input` multiplied by `count` overflows.
This wraps Go's [`strings.Repeat`](https://golang.org/pkg/strings/#Repeat).
### Usage
```go
strings.Repeat count input
```
```go
input | strings.Repeat count
```
#### Example
```console
$ gomplate -i '{{ "hello, world" | strings.Repeat "world" }}jello'
hello, jello
```
## `strings.ReplaceAll`
**Alias:** `replaceAll`
Replaces all occurrences of a given string with another.
### Usage
```go
strings.ReplaceAll old new input
```
```go
input | strings.ReplaceAll old new
```
### Examples
```console
$ gomplate -i '{{ strings.ReplaceAll "." "-" "172.21.1.42" }}'
172-21-1-42
$ gomplate -i '{{ "172.21.1.42" | strings.ReplaceAll "." "-" }}'
172-21-1-42
```
## `strings.Slug`
Creates a a "slug" from a given string - supports unicode correctly. This wraps the [github.com/gosimple/slug](https://github.com/gosimple/slug) package. See [the github.com/gosimple/slug docs](https://godoc.org/github.com/gosimple/slug) for more information.
### Usage
```go
strings.Slug input
```
```go
input | strings.Slug
```
### Examples
```console
$ gomplate -i '{{ "Hello, world!" | strings.Slug }}'
hello-world
$ echo 'Rock & Roll @ Cafe Wha?' | gomplate -d in=stdin: -i '{{ strings.Slug (include "in") }}'
rock-and-roll-at-cafe-wha
```
## `strings.Title`
**Alias:** `title`
Convert to title-case.
### Usage
```go
strings.Title input
```
```go
input | strings.Title
```
### Example
```console
$ gomplate -i '{{strings.Title "hello, world!"}}'
Hello, World!
```
## `strings.ToLower`
**Alias:** `toLower`
Convert to lower-case.
### Usage
```go
strings.ToLower input
```
```go
input | strings.ToLower
```
#### Example
```console
$ echo '{{strings.ToLower "HELLO, WORLD!"}}' | gomplate
hello, world!
```
## `strings.ToUpper`
**Alias:** `toUpper`
Convert to upper-case.
### Usage
```go
strings.ToUpper input
```
```go
input | strings.ToUpper
```
#### Example
```console
$ gomplate -i '{{strings.ToUpper "hello, world!"}}'
HELLO, WORLD!
```
## `strings.Trim`
Trims a string by removing the given characters from the beginning and end of
the string.
### Usage
```go
strings.Trim cutset input
```
```go
input | strings.Trim cutset
```
#### Example
```console
$ gomplate -i '{{ "_-foo-_" | strings.Trim "_-" }}
foo
```
## `strings.TrimPrefix`
Returns a string without the provided leading prefix string, if the prefix is present.
This wraps Go's [`strings.TrimPrefix`](https://golang.org/pkg/strings/#TrimPrefix).
### Usage
```go
strings.TrimPrefix prefix input
```
```go
input | strings.TrimPrefix prefix
```
#### Example
```console
$ gomplate -i '{{ "hello, world" | strings.TrimPrefix "hello, " }}'
world
```
## `strings.TrimSpace`
**Alias:** `trimSpace`
Trims a string by removing whitespace from the beginning and end of
the string.
### Usage
```go
strings.TrimSpace input
```
```go
input | strings.TrimSpace
```
#### Example
```console
$ gomplate -i '{{ " \n\t foo" | strings.TrimSpace }}'
foo
```
## `strings.TrimSuffix`
Returns a string without the provided trailing suffix string, if the suffix is present.
This wraps Go's [`strings.TrimSuffix`](https://golang.org/pkg/strings/#TrimSuffix).
### Usage
```go
strings.TrimSuffix suffix input
```
```go
input | strings.TrimSuffix suffix
```
#### Example
```console
$ gomplate -i '{{ "hello, world" | strings.TrimSuffix "world" }}jello'
hello, jello
```
## `strings.Trunc`
Returns a string truncated to the given length.
_Also see [`strings.Abbrev`](#strings-abbrev)._
### Usage
```go
strings.Trunc length input
```
```go
input | strings.Trunc length
```
#### Example
```console
$ gomplate -i '{{ "hello, world" | strings.Trunc 5 }}'
hello
```
## `contains`
**See [`strings.Contains](#strings-contains) for a pipeline-compatible version**
Contains reports whether the second string is contained within the first. Equivalent to
[strings.Contains](https://golang.org/pkg/strings#Contains)
### Example
_`input.tmpl`:_
```
{{if contains .Env.FOO "f"}}yes{{else}}no{{end}}
```
```console
$ FOO=foo gomplate < input.tmpl
yes
$ FOO=bar gomplate < input.tmpl
no
```
## `hasPrefix`
**See [`strings.HasPrefix](#strings-hasprefix) for a pipeline-compatible version**
Tests whether the string begins with a certain substring. Equivalent to
[strings.HasPrefix](https://golang.org/pkg/strings#HasPrefix)
#### Example
_`input.tmpl`:_
```
{{if hasPrefix .Env.URL "https"}}foo{{else}}bar{{end}}
```
```console
$ URL=http://example.com gomplate < input.tmpl
bar
$ URL=https://example.com gomplate < input.tmpl
foo
```
## `hasSuffix`
**See [`strings.HasSuffix](#strings-hassuffix) for a pipeline-compatible version**
Tests whether the string ends with a certain substring. Equivalent to
[strings.HasSuffix](https://golang.org/pkg/strings#HasSuffix)
#### Example
_`input.tmpl`:_
```
{{.Env.URL}}{{if not (hasSuffix .Env.URL ":80")}}:80{{end}}
```
```console
$ URL=http://example.com gomplate < input.tmpl
http://example.com:80
```
## `split`
**See [`strings.Split](#strings-split) for a pipeline-compatible version**
Creates a slice by splitting a string on a given delimiter. Equivalent to
[strings.Split](https://golang.org/pkg/strings#Split)
#### Example
```console
$ gomplate -i '{{range split "Bart,Lisa,Maggie" ","}}Hello, {{.}}{{end}}'
Hello, Bart
Hello, Lisa
Hello, Maggie
```
## `splitN`
**See [`strings.SplitN](#strings-splitn) for a pipeline-compatible version**
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
```
## `trim`
**See [`strings.Trim](#strings-trim) for a pipeline-compatible version**
Trims a string by removing the given characters from the beginning and end of
the string. Equivalent to [strings.Trim](https://golang.org/pkg/strings/#Trim)
#### Example
_`input.tmpl`:_
```
Hello, {{trim .Env.FOO " "}}!
```
```console
$ FOO=" world " | gomplate < input.tmpl
Hello, world!
```
|