| 1 |
# Template Functions |
| 2 |
|
| 3 |
These template functions may be used in the **[message template](../publish.md#message-templating)** feature of ntfy. Please refer to the examples in the documentation for how to use them. |
| 4 |
|
| 5 |
The original set of template functions is based on the [Sprig library](https://masterminds.github.io/sprig/). This documentation page is a (slightly modified) copy of their docs. **Thank you to the Sprig developers for their work!** 🙏 |
| 6 |
|
| 7 |
## Table of Contents |
| 8 |
|
| 9 |
- [String Functions](#string-functions) |
| 10 |
- [String List Functions](#string-list-functions) |
| 11 |
- [Integer Math Functions](#integer-math-functions) |
| 12 |
- [Integer List Functions](#integer-list-functions) |
| 13 |
- [Float Math Functions](#float-math-functions) |
| 14 |
- [Date Functions](#date-functions) |
| 15 |
- [Default Functions](#default-functions) |
| 16 |
- [Encoding Functions](#encoding-functions) |
| 17 |
- [Lists and List Functions](#lists-and-list-functions) |
| 18 |
- [Dictionaries and Dict Functions](#dictionaries-and-dict-functions) |
| 19 |
- [Type Conversion Functions](#type-conversion-functions) |
| 20 |
- [Path and Filepath Functions](#path-and-filepath-functions) |
| 21 |
- [Flow Control Functions](#flow-control-functions) |
| 22 |
- [Reflection Functions](#reflection-functions) |
| 23 |
- [Cryptographic and Security Functions](#cryptographic-and-security-functions) |
| 24 |
- [URL Functions](#url-functions) |
| 25 |
|
| 26 |
## String Functions |
| 27 |
|
| 28 |
Sprig has a number of string manipulation functions. |
| 29 |
|
| 30 |
### trim |
| 31 |
|
| 32 |
The `trim` function removes space from either side of a string: |
| 33 |
|
| 34 |
``` |
| 35 |
trim " hello " |
| 36 |
``` |
| 37 |
|
| 38 |
The above produces `hello` |
| 39 |
|
| 40 |
### trimAll |
| 41 |
|
| 42 |
Remove given characters from the front or back of a string: |
| 43 |
|
| 44 |
``` |
| 45 |
trimAll "$" "$5.00" |
| 46 |
``` |
| 47 |
|
| 48 |
The above returns `5.00` (as a string). |
| 49 |
|
| 50 |
### trimSuffix |
| 51 |
|
| 52 |
Trim just the suffix from a string: |
| 53 |
|
| 54 |
``` |
| 55 |
trimSuffix "-" "hello-" |
| 56 |
``` |
| 57 |
|
| 58 |
The above returns `hello` |
| 59 |
|
| 60 |
### trimPrefix |
| 61 |
|
| 62 |
Trim just the prefix from a string: |
| 63 |
|
| 64 |
``` |
| 65 |
trimPrefix "-" "-hello" |
| 66 |
``` |
| 67 |
|
| 68 |
The above returns `hello` |
| 69 |
|
| 70 |
### upper |
| 71 |
|
| 72 |
Convert the entire string to uppercase: |
| 73 |
|
| 74 |
``` |
| 75 |
upper "hello" |
| 76 |
``` |
| 77 |
|
| 78 |
The above returns `HELLO` |
| 79 |
|
| 80 |
### lower |
| 81 |
|
| 82 |
Convert the entire string to lowercase: |
| 83 |
|
| 84 |
``` |
| 85 |
lower "HELLO" |
| 86 |
``` |
| 87 |
|
| 88 |
The above returns `hello` |
| 89 |
|
| 90 |
### title |
| 91 |
|
| 92 |
Convert to title case: |
| 93 |
|
| 94 |
``` |
| 95 |
title "hello world" |
| 96 |
``` |
| 97 |
|
| 98 |
The above returns `Hello World` |
| 99 |
|
| 100 |
### repeat |
| 101 |
|
| 102 |
Repeat a string multiple times: |
| 103 |
|
| 104 |
``` |
| 105 |
repeat 3 "hello" |
| 106 |
``` |
| 107 |
|
| 108 |
The above returns `hellohellohello` |
| 109 |
|
| 110 |
### substr |
| 111 |
|
| 112 |
Get a substring from a string. It takes three parameters: |
| 113 |
|
| 114 |
- start (int) |
| 115 |
- end (int) |
| 116 |
- string (string) |
| 117 |
|
| 118 |
``` |
| 119 |
substr 0 5 "hello world" |
| 120 |
``` |
| 121 |
|
| 122 |
The above returns `hello` |
| 123 |
|
| 124 |
### trunc |
| 125 |
|
| 126 |
Truncate a string (and add no suffix) |
| 127 |
|
| 128 |
``` |
| 129 |
trunc 5 "hello world" |
| 130 |
``` |
| 131 |
|
| 132 |
The above produces `hello`. |
| 133 |
|
| 134 |
``` |
| 135 |
trunc -5 "hello world" |
| 136 |
``` |
| 137 |
|
| 138 |
The above produces `world`. |
| 139 |
|
| 140 |
### contains |
| 141 |
|
| 142 |
Test to see if one string is contained inside of another: |
| 143 |
|
| 144 |
``` |
| 145 |
contains "cat" "catch" |
| 146 |
``` |
| 147 |
|
| 148 |
The above returns `true` because `catch` contains `cat`. |
| 149 |
|
| 150 |
### hasPrefix and hasSuffix |
| 151 |
|
| 152 |
The `hasPrefix` and `hasSuffix` functions test whether a string has a given |
| 153 |
prefix or suffix: |
| 154 |
|
| 155 |
``` |
| 156 |
hasPrefix "cat" "catch" |
| 157 |
``` |
| 158 |
|
| 159 |
The above returns `true` because `catch` has the prefix `cat`. |
| 160 |
|
| 161 |
### quote and squote |
| 162 |
|
| 163 |
These functions wrap a string in double quotes (`quote`) or single quotes |
| 164 |
(`squote`). |
| 165 |
|
| 166 |
### cat |
| 167 |
|
| 168 |
The `cat` function concatenates multiple strings together into one, separating |
| 169 |
them with spaces: |
| 170 |
|
| 171 |
``` |
| 172 |
cat "hello" "beautiful" "world" |
| 173 |
``` |
| 174 |
|
| 175 |
The above produces `hello beautiful world` |
| 176 |
|
| 177 |
### indent |
| 178 |
|
| 179 |
The `indent` function indents every line in a given string to the specified |
| 180 |
indent width. This is useful when aligning multi-line strings: |
| 181 |
|
| 182 |
``` |
| 183 |
indent 4 $lots_of_text |
| 184 |
``` |
| 185 |
|
| 186 |
The above will indent every line of text by 4 space characters. |
| 187 |
|
| 188 |
### nindent |
| 189 |
|
| 190 |
The `nindent` function is the same as the indent function, but prepends a new |
| 191 |
line to the beginning of the string. |
| 192 |
|
| 193 |
``` |
| 194 |
nindent 4 $lots_of_text |
| 195 |
``` |
| 196 |
|
| 197 |
The above will indent every line of text by 4 space characters and add a new |
| 198 |
line to the beginning. |
| 199 |
|
| 200 |
### replace |
| 201 |
|
| 202 |
Perform simple string replacement. |
| 203 |
|
| 204 |
It takes three arguments: |
| 205 |
|
| 206 |
- string to replace |
| 207 |
- string to replace with |
| 208 |
- source string |
| 209 |
|
| 210 |
``` |
| 211 |
"I Am Henry VIII" | replace " " "-" |
| 212 |
``` |
| 213 |
|
| 214 |
The above will produce `I-Am-Henry-VIII` |
| 215 |
|
| 216 |
### plural |
| 217 |
|
| 218 |
Pluralize a string. |
| 219 |
|
| 220 |
``` |
| 221 |
len $fish | plural "one anchovy" "many anchovies" |
| 222 |
``` |
| 223 |
|
| 224 |
In the above, if the length of the string is 1, the first argument will be |
| 225 |
printed (`one anchovy`). Otherwise, the second argument will be printed |
| 226 |
(`many anchovies`). |
| 227 |
|
| 228 |
The arguments are: |
| 229 |
|
| 230 |
- singular string |
| 231 |
- plural string |
| 232 |
- length integer |
| 233 |
|
| 234 |
NOTE: Sprig does not currently support languages with more complex pluralization |
| 235 |
rules. And `0` is considered a plural because the English language treats it |
| 236 |
as such (`zero anchovies`). The Sprig developers are working on a solution for |
| 237 |
better internationalization. |
| 238 |
|
| 239 |
### regexMatch, mustRegexMatch |
| 240 |
|
| 241 |
Returns true if the input string contains any match of the regular expression. |
| 242 |
|
| 243 |
``` |
| 244 |
regexMatch "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" "[email protected]" |
| 245 |
``` |
| 246 |
|
| 247 |
The above produces `true` |
| 248 |
|
| 249 |
`regexMatch` panics if there is a problem and `mustRegexMatch` returns an error to the |
| 250 |
template engine if there is a problem. |
| 251 |
|
| 252 |
### regexFindAll, mustRegexFindAll |
| 253 |
|
| 254 |
Returns a slice of all matches of the regular expression in the input string. |
| 255 |
The last parameter n determines the number of substrings to return, where -1 means return all matches |
| 256 |
|
| 257 |
``` |
| 258 |
regexFindAll "[2,4,6,8]" "123456789" -1 |
| 259 |
``` |
| 260 |
|
| 261 |
The above produces `[2 4 6 8]` |
| 262 |
|
| 263 |
`regexFindAll` panics if there is a problem and `mustRegexFindAll` returns an error to the |
| 264 |
template engine if there is a problem. |
| 265 |
|
| 266 |
### regexFind, mustRegexFind |
| 267 |
|
| 268 |
Return the first (left most) match of the regular expression in the input string |
| 269 |
|
| 270 |
``` |
| 271 |
regexFind "[a-zA-Z][1-9]" "abcd1234" |
| 272 |
``` |
| 273 |
|
| 274 |
The above produces `d1` |
| 275 |
|
| 276 |
`regexFind` panics if there is a problem and `mustRegexFind` returns an error to the |
| 277 |
template engine if there is a problem. |
| 278 |
|
| 279 |
### regexReplaceAll, mustRegexReplaceAll |
| 280 |
|
| 281 |
Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. |
| 282 |
Inside string replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch |
| 283 |
|
| 284 |
``` |
| 285 |
regexReplaceAll "a(x*)b" "-ab-axxb-" "${1}W" |
| 286 |
``` |
| 287 |
|
| 288 |
The above produces `-W-xxW-` |
| 289 |
|
| 290 |
`regexReplaceAll` panics if there is a problem and `mustRegexReplaceAll` returns an error to the |
| 291 |
template engine if there is a problem. |
| 292 |
|
| 293 |
### regexReplaceAllLiteral, mustRegexReplaceAllLiteral |
| 294 |
|
| 295 |
Returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement |
| 296 |
The replacement string is substituted directly, without using Expand |
| 297 |
|
| 298 |
``` |
| 299 |
regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}" |
| 300 |
``` |
| 301 |
|
| 302 |
The above produces `-${1}-${1}-` |
| 303 |
|
| 304 |
`regexReplaceAllLiteral` panics if there is a problem and `mustRegexReplaceAllLiteral` returns an error to the |
| 305 |
template engine if there is a problem. |
| 306 |
|
| 307 |
### regexSplit, mustRegexSplit |
| 308 |
|
| 309 |
Slices the input string into substrings separated by the expression and returns a slice of the substrings between those expression matches. The last parameter `n` determines the number of substrings to return, where `-1` means return all matches |
| 310 |
|
| 311 |
``` |
| 312 |
regexSplit "z+" "pizza" -1 |
| 313 |
``` |
| 314 |
|
| 315 |
The above produces `[pi a]` |
| 316 |
|
| 317 |
`regexSplit` panics if there is a problem and `mustRegexSplit` returns an error to the |
| 318 |
template engine if there is a problem. |
| 319 |
|
| 320 |
### regexQuoteMeta |
| 321 |
|
| 322 |
Returns a string that escapes all regular expression metacharacters inside the argument text; |
| 323 |
the returned string is a regular expression matching the literal text. |
| 324 |
|
| 325 |
``` |
| 326 |
regexQuoteMeta "1.2.3" |
| 327 |
``` |
| 328 |
|
| 329 |
The above produces `1\.2\.3` |
| 330 |
|
| 331 |
### See Also... |
| 332 |
|
| 333 |
The [Conversion Functions](#type-conversion-functions) contain functions for converting strings. The [String List Functions](#string-list-functions) contains |
| 334 |
functions for working with an array of strings. |
| 335 |
|
| 336 |
## String List Functions |
| 337 |
|
| 338 |
These functions operate on or generate slices of strings. In Go, a slice is a |
| 339 |
growable array. In Sprig, it's a special case of a `list`. |
| 340 |
|
| 341 |
### join |
| 342 |
|
| 343 |
Join a list of strings into a single string, with the given separator. |
| 344 |
|
| 345 |
``` |
| 346 |
list "hello" "world" | join "_" |
| 347 |
``` |
| 348 |
|
| 349 |
The above will produce `hello_world` |
| 350 |
|
| 351 |
`join` will try to convert non-strings to a string value: |
| 352 |
|
| 353 |
``` |
| 354 |
list 1 2 3 | join "+" |
| 355 |
``` |
| 356 |
|
| 357 |
The above will produce `1+2+3` |
| 358 |
|
| 359 |
### splitList and split |
| 360 |
|
| 361 |
Split a string into a list of strings: |
| 362 |
|
| 363 |
``` |
| 364 |
splitList "$" "foo$bar$baz" |
| 365 |
``` |
| 366 |
|
| 367 |
The above will return `[foo bar baz]` |
| 368 |
|
| 369 |
The older `split` function splits a string into a `dict`. It is designed to make |
| 370 |
it easy to use template dot notation for accessing members: |
| 371 |
|
| 372 |
``` |
| 373 |
$a := split "$" "foo$bar$baz" |
| 374 |
``` |
| 375 |
|
| 376 |
The above produces a map with index keys. `{_0: foo, _1: bar, _2: baz}` |
| 377 |
|
| 378 |
``` |
| 379 |
$a._0 |
| 380 |
``` |
| 381 |
|
| 382 |
The above produces `foo` |
| 383 |
|
| 384 |
### splitn |
| 385 |
|
| 386 |
`splitn` function splits a string into a `dict` with `n` keys. It is designed to make |
| 387 |
it easy to use template dot notation for accessing members: |
| 388 |
|
| 389 |
``` |
| 390 |
$a := splitn "$" 2 "foo$bar$baz" |
| 391 |
``` |
| 392 |
|
| 393 |
The above produces a map with index keys. `{_0: foo, _1: bar$baz}` |
| 394 |
|
| 395 |
``` |
| 396 |
$a._0 |
| 397 |
``` |
| 398 |
|
| 399 |
The above produces `foo` |
| 400 |
|
| 401 |
### sortAlpha |
| 402 |
|
| 403 |
The `sortAlpha` function sorts a list of strings into alphabetical (lexicographical) |
| 404 |
order. |
| 405 |
|
| 406 |
It does _not_ sort in place, but returns a sorted copy of the list, in keeping |
| 407 |
with the immutability of lists. |
| 408 |
|
| 409 |
## Integer Math Functions |
| 410 |
|
| 411 |
The following math functions operate on `int64` values. |
| 412 |
|
| 413 |
### add |
| 414 |
|
| 415 |
Sum numbers with `add`. Accepts two or more inputs. |
| 416 |
|
| 417 |
``` |
| 418 |
add 1 2 3 |
| 419 |
``` |
| 420 |
|
| 421 |
### add1 |
| 422 |
|
| 423 |
To increment by 1, use `add1` |
| 424 |
|
| 425 |
### sub |
| 426 |
|
| 427 |
To subtract, use `sub` |
| 428 |
|
| 429 |
### div |
| 430 |
|
| 431 |
Perform integer division with `div` |
| 432 |
|
| 433 |
### mod |
| 434 |
|
| 435 |
Modulo with `mod` |
| 436 |
|
| 437 |
### mul |
| 438 |
|
| 439 |
Multiply with `mul`. Accepts two or more inputs. |
| 440 |
|
| 441 |
``` |
| 442 |
mul 1 2 3 |
| 443 |
``` |
| 444 |
|
| 445 |
### max |
| 446 |
|
| 447 |
Return the largest of a series of integers: |
| 448 |
|
| 449 |
This will return `3`: |
| 450 |
|
| 451 |
``` |
| 452 |
max 1 2 3 |
| 453 |
``` |
| 454 |
|
| 455 |
### min |
| 456 |
|
| 457 |
Return the smallest of a series of integers. |
| 458 |
|
| 459 |
`min 1 2 3` will return `1` |
| 460 |
|
| 461 |
### floor |
| 462 |
|
| 463 |
Returns the greatest float value less than or equal to input value |
| 464 |
|
| 465 |
`floor 123.9999` will return `123.0` |
| 466 |
|
| 467 |
### ceil |
| 468 |
|
| 469 |
Returns the greatest float value greater than or equal to input value |
| 470 |
|
| 471 |
`ceil 123.001` will return `124.0` |
| 472 |
|
| 473 |
### round |
| 474 |
|
| 475 |
Returns a float value with the remainder rounded to the given number to digits after the decimal point. |
| 476 |
|
| 477 |
`round 123.555555 3` will return `123.556` |
| 478 |
|
| 479 |
### randInt |
| 480 |
Returns a random integer value from min (inclusive) to max (exclusive). |
| 481 |
|
| 482 |
``` |
| 483 |
randInt 12 30 |
| 484 |
``` |
| 485 |
|
| 486 |
The above will produce a random number in the range [12,30]. |
| 487 |
|
| 488 |
## Integer List Functions |
| 489 |
|
| 490 |
### until |
| 491 |
|
| 492 |
The `until` function builds a range of integers. |
| 493 |
|
| 494 |
``` |
| 495 |
until 5 |
| 496 |
``` |
| 497 |
|
| 498 |
The above generates the list `[0, 1, 2, 3, 4]`. |
| 499 |
|
| 500 |
This is useful for looping with `range $i, $e := until 5`. |
| 501 |
|
| 502 |
### untilStep |
| 503 |
|
| 504 |
Like `until`, `untilStep` generates a list of counting integers. But it allows |
| 505 |
you to define a start, stop, and step: |
| 506 |
|
| 507 |
``` |
| 508 |
untilStep 3 6 2 |
| 509 |
``` |
| 510 |
|
| 511 |
The above will produce `[3 5]` by starting with 3, and adding 2 until it is equal |
| 512 |
or greater than 6. This is similar to Python's `range` function. |
| 513 |
|
| 514 |
### seq |
| 515 |
|
| 516 |
Works like the bash `seq` command. |
| 517 |
* 1 parameter (end) - will generate all counting integers between 1 and `end` inclusive. |
| 518 |
* 2 parameters (start, end) - will generate all counting integers between `start` and `end` inclusive incrementing or decrementing by 1. |
| 519 |
* 3 parameters (start, step, end) - will generate all counting integers between `start` and `end` inclusive incrementing or decrementing by `step`. |
| 520 |
|
| 521 |
``` |
| 522 |
seq 5 => 1 2 3 4 5 |
| 523 |
seq -3 => 1 0 -1 -2 -3 |
| 524 |
seq 0 2 => 0 1 2 |
| 525 |
seq 2 -2 => 2 1 0 -1 -2 |
| 526 |
seq 0 2 10 => 0 2 4 6 8 10 |
| 527 |
seq 0 -2 -5 => 0 -2 -4 |
| 528 |
``` |
| 529 |
|
| 530 |
## Float Math Functions |
| 531 |
|
| 532 |
### maxf |
| 533 |
|
| 534 |
Return the largest of a series of floats: |
| 535 |
|
| 536 |
This will return `3`: |
| 537 |
|
| 538 |
``` |
| 539 |
maxf 1 2.5 3 |
| 540 |
``` |
| 541 |
|
| 542 |
### minf |
| 543 |
|
| 544 |
Return the smallest of a series of floats. |
| 545 |
|
| 546 |
This will return `1.5`: |
| 547 |
|
| 548 |
``` |
| 549 |
minf 1.5 2 3 |
| 550 |
``` |
| 551 |
|
| 552 |
## Date Functions |
| 553 |
|
| 554 |
### now |
| 555 |
|
| 556 |
The current date/time. Use this in conjunction with other date functions. |
| 557 |
|
| 558 |
### ago |
| 559 |
|
| 560 |
The `ago` function returns duration from time.Now in seconds resolution. |
| 561 |
|
| 562 |
``` |
| 563 |
ago .CreatedAt |
| 564 |
``` |
| 565 |
|
| 566 |
returns in `time.Duration` String() format |
| 567 |
|
| 568 |
``` |
| 569 |
2h34m7s |
| 570 |
``` |
| 571 |
|
| 572 |
### date |
| 573 |
|
| 574 |
The `date` function formats a date. |
| 575 |
|
| 576 |
Format the date to YEAR-MONTH-DAY: |
| 577 |
|
| 578 |
``` |
| 579 |
now | date "2006-01-02" |
| 580 |
``` |
| 581 |
|
| 582 |
Date formatting in Go is a [little bit different](https://pauladamsmith.com/blog/2011/05/go_time.html). |
| 583 |
|
| 584 |
In short, take this as the base date: |
| 585 |
|
| 586 |
``` |
| 587 |
Mon Jan 2 15:04:05 MST 2006 |
| 588 |
``` |
| 589 |
|
| 590 |
Write it in the format you want. Above, `2006-01-02` is the same date, but |
| 591 |
in the format we want. |
| 592 |
|
| 593 |
### dateInZone |
| 594 |
|
| 595 |
Same as `date`, but with a timezone. |
| 596 |
|
| 597 |
``` |
| 598 |
dateInZone "2006-01-02" (now) "UTC" |
| 599 |
``` |
| 600 |
|
| 601 |
### duration |
| 602 |
|
| 603 |
Formats a given amount of seconds as a `time.Duration`. |
| 604 |
|
| 605 |
This returns 1m35s |
| 606 |
|
| 607 |
``` |
| 608 |
duration "95" |
| 609 |
``` |
| 610 |
|
| 611 |
### durationRound |
| 612 |
|
| 613 |
Rounds a given duration to the most significant unit. Strings and `time.Duration` |
| 614 |
gets parsed as a duration, while a `time.Time` is calculated as the duration since. |
| 615 |
|
| 616 |
This return 2h |
| 617 |
|
| 618 |
``` |
| 619 |
durationRound "2h10m5s" |
| 620 |
``` |
| 621 |
|
| 622 |
This returns 3mo |
| 623 |
|
| 624 |
``` |
| 625 |
durationRound "2400h10m5s" |
| 626 |
``` |
| 627 |
|
| 628 |
### unixEpoch |
| 629 |
|
| 630 |
Returns the seconds since the unix epoch for a `time.Time`. |
| 631 |
|
| 632 |
``` |
| 633 |
now | unixEpoch |
| 634 |
``` |
| 635 |
|
| 636 |
### dateModify, mustDateModify |
| 637 |
|
| 638 |
The `dateModify` takes a modification and a date and returns the timestamp. |
| 639 |
|
| 640 |
Subtract an hour and thirty minutes from the current time: |
| 641 |
|
| 642 |
``` |
| 643 |
now | dateModify "-1.5h" |
| 644 |
``` |
| 645 |
|
| 646 |
If the modification format is wrong `dateModify` will return the date unmodified. `mustDateModify` will return an error otherwise. |
| 647 |
|
| 648 |
### htmlDate |
| 649 |
|
| 650 |
The `htmlDate` function formats a date for inserting into an HTML date picker |
| 651 |
input field. |
| 652 |
|
| 653 |
``` |
| 654 |
now | htmlDate |
| 655 |
``` |
| 656 |
|
| 657 |
### htmlDateInZone |
| 658 |
|
| 659 |
Same as htmlDate, but with a timezone. |
| 660 |
|
| 661 |
``` |
| 662 |
htmlDateInZone (now) "UTC" |
| 663 |
``` |
| 664 |
|
| 665 |
### toDate, mustToDate |
| 666 |
|
| 667 |
`toDate` converts a string to a date. The first argument is the date layout and |
| 668 |
the second the date string. If the string can't be convert it returns the zero |
| 669 |
value. |
| 670 |
`mustToDate` will return an error in case the string cannot be converted. |
| 671 |
|
| 672 |
This is useful when you want to convert a string date to another format |
| 673 |
(using pipe). The example below converts "2017-12-31" to "31/12/2017". |
| 674 |
|
| 675 |
``` |
| 676 |
toDate "2006-01-02" "2017-12-31" | date "02/01/2006" |
| 677 |
``` |
| 678 |
|
| 679 |
## Default Functions |
| 680 |
|
| 681 |
Sprig provides tools for setting default values for templates. |
| 682 |
|
| 683 |
### default |
| 684 |
|
| 685 |
To set a simple default value, use `default`: |
| 686 |
|
| 687 |
``` |
| 688 |
default "foo" .Bar |
| 689 |
``` |
| 690 |
|
| 691 |
In the above, if `.Bar` evaluates to a non-empty value, it will be used. But if |
| 692 |
it is empty, `foo` will be returned instead. |
| 693 |
|
| 694 |
The definition of "empty" depends on type: |
| 695 |
|
| 696 |
- Numeric: 0 |
| 697 |
- String: "" |
| 698 |
- Lists: `[]` |
| 699 |
- Dicts: `{}` |
| 700 |
- Boolean: `false` |
| 701 |
- And always `nil` (aka null) |
| 702 |
|
| 703 |
For structs, there is no definition of empty, so a struct will never return the |
| 704 |
default. |
| 705 |
|
| 706 |
### empty |
| 707 |
|
| 708 |
The `empty` function returns `true` if the given value is considered empty, and |
| 709 |
`false` otherwise. The empty values are listed in the `default` section. |
| 710 |
|
| 711 |
``` |
| 712 |
empty .Foo |
| 713 |
``` |
| 714 |
|
| 715 |
Note that in Go template conditionals, emptiness is calculated for you. Thus, |
| 716 |
you rarely need `if empty .Foo`. Instead, just use `if .Foo`. |
| 717 |
|
| 718 |
### coalesce |
| 719 |
|
| 720 |
The `coalesce` function takes a list of values and returns the first non-empty |
| 721 |
one. |
| 722 |
|
| 723 |
``` |
| 724 |
coalesce 0 1 2 |
| 725 |
``` |
| 726 |
|
| 727 |
The above returns `1`. |
| 728 |
|
| 729 |
This function is useful for scanning through multiple variables or values: |
| 730 |
|
| 731 |
``` |
| 732 |
coalesce .name .parent.name "Matt" |
| 733 |
``` |
| 734 |
|
| 735 |
The above will first check to see if `.name` is empty. If it is not, it will return |
| 736 |
that value. If it _is_ empty, `coalesce` will evaluate `.parent.name` for emptiness. |
| 737 |
Finally, if both `.name` and `.parent.name` are empty, it will return `Matt`. |
| 738 |
|
| 739 |
### all |
| 740 |
|
| 741 |
The `all` function takes a list of values and returns true if all values are non-empty. |
| 742 |
|
| 743 |
``` |
| 744 |
all 0 1 2 |
| 745 |
``` |
| 746 |
|
| 747 |
The above returns `false`. |
| 748 |
|
| 749 |
This function is useful for evaluating multiple conditions of variables or values: |
| 750 |
|
| 751 |
``` |
| 752 |
all (eq .Request.TLS.Version 0x0304) (.Request.ProtoAtLeast 2 0) (eq .Request.Method "POST") |
| 753 |
``` |
| 754 |
|
| 755 |
The above will check http.Request is POST with tls 1.3 and http/2. |
| 756 |
|
| 757 |
### any |
| 758 |
|
| 759 |
The `any` function takes a list of values and returns true if any value is non-empty. |
| 760 |
|
| 761 |
``` |
| 762 |
any 0 1 2 |
| 763 |
``` |
| 764 |
|
| 765 |
The above returns `true`. |
| 766 |
|
| 767 |
This function is useful for evaluating multiple conditions of variables or values: |
| 768 |
|
| 769 |
``` |
| 770 |
any (eq .Request.Method "GET") (eq .Request.Method "POST") (eq .Request.Method "OPTIONS") |
| 771 |
``` |
| 772 |
|
| 773 |
The above will check http.Request method is one of GET/POST/OPTIONS. |
| 774 |
|
| 775 |
### fromJSON, mustFromJSON |
| 776 |
|
| 777 |
`fromJSON` decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string. |
| 778 |
`mustFromJSON` will return an error in case the JSON is invalid. |
| 779 |
|
| 780 |
``` |
| 781 |
fromJSON "{\"foo\": 55}" |
| 782 |
``` |
| 783 |
|
| 784 |
### toJSON, mustToJSON |
| 785 |
|
| 786 |
The `toJSON` function encodes an item into a JSON string. If the item cannot be converted to JSON the function will return an empty string. |
| 787 |
`mustToJSON` will return an error in case the item cannot be encoded in JSON. |
| 788 |
|
| 789 |
``` |
| 790 |
toJSON .Item |
| 791 |
``` |
| 792 |
|
| 793 |
The above returns JSON string representation of `.Item`. |
| 794 |
|
| 795 |
### toPrettyJSON, mustToPrettyJSON |
| 796 |
|
| 797 |
The `toPrettyJSON` function encodes an item into a pretty (indented) JSON string. |
| 798 |
|
| 799 |
``` |
| 800 |
toPrettyJSON .Item |
| 801 |
``` |
| 802 |
|
| 803 |
The above returns indented JSON string representation of `.Item`. |
| 804 |
|
| 805 |
### toRawJSON, mustToRawJSON |
| 806 |
|
| 807 |
The `toRawJSON` function encodes an item into JSON string with HTML characters unescaped. |
| 808 |
|
| 809 |
``` |
| 810 |
toRawJSON .Item |
| 811 |
``` |
| 812 |
|
| 813 |
The above returns unescaped JSON string representation of `.Item`. |
| 814 |
|
| 815 |
### ternary |
| 816 |
|
| 817 |
The `ternary` function takes two values, and a test value. If the test value is |
| 818 |
true, the first value will be returned. If the test value is empty, the second |
| 819 |
value will be returned. This is similar to the c ternary operator. |
| 820 |
|
| 821 |
#### true test value |
| 822 |
|
| 823 |
``` |
| 824 |
ternary "foo" "bar" true |
| 825 |
``` |
| 826 |
|
| 827 |
or |
| 828 |
|
| 829 |
``` |
| 830 |
true | ternary "foo" "bar" |
| 831 |
``` |
| 832 |
|
| 833 |
The above returns `"foo"`. |
| 834 |
|
| 835 |
#### false test value |
| 836 |
|
| 837 |
``` |
| 838 |
ternary "foo" "bar" false |
| 839 |
``` |
| 840 |
|
| 841 |
or |
| 842 |
|
| 843 |
``` |
| 844 |
false | ternary "foo" "bar" |
| 845 |
``` |
| 846 |
|
| 847 |
The above returns `"bar"`. |
| 848 |
|
| 849 |
## Encoding Functions |
| 850 |
|
| 851 |
Sprig has the following encoding and decoding functions: |
| 852 |
|
| 853 |
- `b64enc`/`b64dec`: Encode or decode with Base64 |
| 854 |
- `b32enc`/`b32dec`: Encode or decode with Base32 |
| 855 |
|
| 856 |
## Lists and List Functions |
| 857 |
|
| 858 |
Sprig provides a simple `list` type that can contain arbitrary sequential lists |
| 859 |
of data. This is similar to arrays or slices, but lists are designed to be used |
| 860 |
as immutable data types. |
| 861 |
|
| 862 |
Create a list of integers: |
| 863 |
|
| 864 |
``` |
| 865 |
$myList := list 1 2 3 4 5 |
| 866 |
``` |
| 867 |
|
| 868 |
The above creates a list of `[1 2 3 4 5]`. |
| 869 |
|
| 870 |
### first, mustFirst |
| 871 |
|
| 872 |
To get the head item on a list, use `first`. |
| 873 |
|
| 874 |
`first $myList` returns `1` |
| 875 |
|
| 876 |
`first` panics if there is a problem while `mustFirst` returns an error to the |
| 877 |
template engine if there is a problem. |
| 878 |
|
| 879 |
### rest, mustRest |
| 880 |
|
| 881 |
To get the tail of the list (everything but the first item), use `rest`. |
| 882 |
|
| 883 |
`rest $myList` returns `[2 3 4 5]` |
| 884 |
|
| 885 |
`rest` panics if there is a problem while `mustRest` returns an error to the |
| 886 |
template engine if there is a problem. |
| 887 |
|
| 888 |
### last, mustLast |
| 889 |
|
| 890 |
To get the last item on a list, use `last`: |
| 891 |
|
| 892 |
`last $myList` returns `5`. This is roughly analogous to reversing a list and |
| 893 |
then calling `first`. |
| 894 |
|
| 895 |
`last` panics if there is a problem while `mustLast` returns an error to the |
| 896 |
template engine if there is a problem. |
| 897 |
|
| 898 |
### initial, mustInitial |
| 899 |
|
| 900 |
This compliments `last` by returning all _but_ the last element. |
| 901 |
`initial $myList` returns `[1 2 3 4]`. |
| 902 |
|
| 903 |
`initial` panics if there is a problem while `mustInitial` returns an error to the |
| 904 |
template engine if there is a problem. |
| 905 |
|
| 906 |
### append, mustAppend |
| 907 |
|
| 908 |
Append a new item to an existing list, creating a new list. |
| 909 |
|
| 910 |
``` |
| 911 |
$new = append $myList 6 |
| 912 |
``` |
| 913 |
|
| 914 |
The above would set `$new` to `[1 2 3 4 5 6]`. `$myList` would remain unaltered. |
| 915 |
|
| 916 |
`append` panics if there is a problem while `mustAppend` returns an error to the |
| 917 |
template engine if there is a problem. |
| 918 |
|
| 919 |
### prepend, mustPrepend |
| 920 |
|
| 921 |
Push an element onto the front of a list, creating a new list. |
| 922 |
|
| 923 |
``` |
| 924 |
prepend $myList 0 |
| 925 |
``` |
| 926 |
|
| 927 |
The above would produce `[0 1 2 3 4 5]`. `$myList` would remain unaltered. |
| 928 |
|
| 929 |
`prepend` panics if there is a problem while `mustPrepend` returns an error to the |
| 930 |
template engine if there is a problem. |
| 931 |
|
| 932 |
### concat |
| 933 |
|
| 934 |
Concatenate arbitrary number of lists into one. |
| 935 |
|
| 936 |
``` |
| 937 |
concat $myList ( list 6 7 ) ( list 8 ) |
| 938 |
``` |
| 939 |
|
| 940 |
The above would produce `[1 2 3 4 5 6 7 8]`. `$myList` would remain unaltered. |
| 941 |
|
| 942 |
### reverse, mustReverse |
| 943 |
|
| 944 |
Produce a new list with the reversed elements of the given list. |
| 945 |
|
| 946 |
``` |
| 947 |
reverse $myList |
| 948 |
``` |
| 949 |
|
| 950 |
The above would generate the list `[5 4 3 2 1]`. |
| 951 |
|
| 952 |
`reverse` panics if there is a problem while `mustReverse` returns an error to the |
| 953 |
template engine if there is a problem. |
| 954 |
|
| 955 |
### uniq, mustUniq |
| 956 |
|
| 957 |
Generate a list with all of the duplicates removed. |
| 958 |
|
| 959 |
``` |
| 960 |
list 1 1 1 2 | uniq |
| 961 |
``` |
| 962 |
|
| 963 |
The above would produce `[1 2]` |
| 964 |
|
| 965 |
`uniq` panics if there is a problem while `mustUniq` returns an error to the |
| 966 |
template engine if there is a problem. |
| 967 |
|
| 968 |
### without, mustWithout |
| 969 |
|
| 970 |
The `without` function filters items out of a list. |
| 971 |
|
| 972 |
``` |
| 973 |
without $myList 3 |
| 974 |
``` |
| 975 |
|
| 976 |
The above would produce `[1 2 4 5]` |
| 977 |
|
| 978 |
Without can take more than one filter: |
| 979 |
|
| 980 |
``` |
| 981 |
without $myList 1 3 5 |
| 982 |
``` |
| 983 |
|
| 984 |
That would produce `[2 4]` |
| 985 |
|
| 986 |
`without` panics if there is a problem while `mustWithout` returns an error to the |
| 987 |
template engine if there is a problem. |
| 988 |
|
| 989 |
### has, mustHas |
| 990 |
|
| 991 |
Test to see if a list has a particular element. |
| 992 |
|
| 993 |
``` |
| 994 |
has 4 $myList |
| 995 |
``` |
| 996 |
|
| 997 |
The above would return `true`, while `has "hello" $myList` would return false. |
| 998 |
|
| 999 |
`has` panics if there is a problem while `mustHas` returns an error to the |
| 1000 |
template engine if there is a problem. |
| 1001 |
|
| 1002 |
### compact, mustCompact |
| 1003 |
|
| 1004 |
Accepts a list and removes entries with empty values. |
| 1005 |
|
| 1006 |
``` |
| 1007 |
$list := list 1 "a" "foo" "" |
| 1008 |
$copy := compact $list |
| 1009 |
``` |
| 1010 |
|
| 1011 |
`compact` will return a new list with the empty (i.e., "") item removed. |
| 1012 |
|
| 1013 |
`compact` panics if there is a problem and `mustCompact` returns an error to the |
| 1014 |
template engine if there is a problem. |
| 1015 |
|
| 1016 |
### slice, mustSlice |
| 1017 |
|
| 1018 |
To get partial elements of a list, use `slice list [n] [m]`. It is |
| 1019 |
equivalent of `list[n:m]`. |
| 1020 |
|
| 1021 |
- `slice $myList` returns `[1 2 3 4 5]`. It is same as `myList[:]`. |
| 1022 |
- `slice $myList 3` returns `[4 5]`. It is same as `myList[3:]`. |
| 1023 |
- `slice $myList 1 3` returns `[2 3]`. It is same as `myList[1:3]`. |
| 1024 |
- `slice $myList 0 3` returns `[1 2 3]`. It is same as `myList[:3]`. |
| 1025 |
|
| 1026 |
`slice` panics if there is a problem while `mustSlice` returns an error to the |
| 1027 |
template engine if there is a problem. |
| 1028 |
|
| 1029 |
### chunk |
| 1030 |
|
| 1031 |
To split a list into chunks of given size, use `chunk size list`. This is useful for pagination. |
| 1032 |
|
| 1033 |
``` |
| 1034 |
chunk 3 (list 1 2 3 4 5 6 7 8) |
| 1035 |
``` |
| 1036 |
|
| 1037 |
This produces list of lists `[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 ] ]`. |
| 1038 |
|
| 1039 |
### A Note on List Internals |
| 1040 |
|
| 1041 |
A list is implemented in Go as a `[]any`. For Go developers embedding |
| 1042 |
Sprig, you may pass `[]any` items into your template context and be |
| 1043 |
able to use all of the `list` functions on those items. |
| 1044 |
|
| 1045 |
## Dictionaries and Dict Functions |
| 1046 |
|
| 1047 |
Sprig provides a key/value storage type called a `dict` (short for "dictionary", |
| 1048 |
as in Python). A `dict` is an _unorder_ type. |
| 1049 |
|
| 1050 |
The key to a dictionary **must be a string**. However, the value can be any |
| 1051 |
type, even another `dict` or `list`. |
| 1052 |
|
| 1053 |
Unlike `list`s, `dict`s are not immutable. The `set` and `unset` functions will |
| 1054 |
modify the contents of a dictionary. |
| 1055 |
|
| 1056 |
### dict |
| 1057 |
|
| 1058 |
Creating dictionaries is done by calling the `dict` function and passing it a |
| 1059 |
list of pairs. |
| 1060 |
|
| 1061 |
The following creates a dictionary with three items: |
| 1062 |
|
| 1063 |
``` |
| 1064 |
$myDict := dict "name1" "value1" "name2" "value2" "name3" "value 3" |
| 1065 |
``` |
| 1066 |
|
| 1067 |
### get |
| 1068 |
|
| 1069 |
Given a map and a key, get the value from the map. |
| 1070 |
|
| 1071 |
``` |
| 1072 |
get $myDict "name1" |
| 1073 |
``` |
| 1074 |
|
| 1075 |
The above returns `"value1"` |
| 1076 |
|
| 1077 |
Note that if the key is not found, this operation will simply return `""`. No error |
| 1078 |
will be generated. |
| 1079 |
|
| 1080 |
### set |
| 1081 |
|
| 1082 |
Use `set` to add a new key/value pair to a dictionary. |
| 1083 |
|
| 1084 |
``` |
| 1085 |
$_ := set $myDict "name4" "value4" |
| 1086 |
``` |
| 1087 |
|
| 1088 |
Note that `set` _returns the dictionary_ (a requirement of Go template functions), |
| 1089 |
so you may need to trap the value as done above with the `$_` assignment. |
| 1090 |
|
| 1091 |
### unset |
| 1092 |
|
| 1093 |
Given a map and a key, delete the key from the map. |
| 1094 |
|
| 1095 |
``` |
| 1096 |
$_ := unset $myDict "name4" |
| 1097 |
``` |
| 1098 |
|
| 1099 |
As with `set`, this returns the dictionary. |
| 1100 |
|
| 1101 |
Note that if the key is not found, this operation will simply return. No error |
| 1102 |
will be generated. |
| 1103 |
|
| 1104 |
### hasKey |
| 1105 |
|
| 1106 |
The `hasKey` function returns `true` if the given dict contains the given key. |
| 1107 |
|
| 1108 |
``` |
| 1109 |
hasKey $myDict "name1" |
| 1110 |
``` |
| 1111 |
|
| 1112 |
If the key is not found, this returns `false`. |
| 1113 |
|
| 1114 |
### pluck |
| 1115 |
|
| 1116 |
The `pluck` function makes it possible to give one key and multiple maps, and |
| 1117 |
get a list of all of the matches: |
| 1118 |
|
| 1119 |
``` |
| 1120 |
pluck "name1" $myDict $myOtherDict |
| 1121 |
``` |
| 1122 |
|
| 1123 |
The above will return a `list` containing every found value (`[value1 otherValue1]`). |
| 1124 |
|
| 1125 |
If the give key is _not found_ in a map, that map will not have an item in the |
| 1126 |
list (and the length of the returned list will be less than the number of dicts |
| 1127 |
in the call to `pluck`. |
| 1128 |
|
| 1129 |
If the key is _found_ but the value is an empty value, that value will be |
| 1130 |
inserted. |
| 1131 |
|
| 1132 |
A common idiom in Sprig templates is to uses `pluck... | first` to get the first |
| 1133 |
matching key out of a collection of dictionaries. |
| 1134 |
|
| 1135 |
### dig |
| 1136 |
|
| 1137 |
The `dig` function traverses a nested set of dicts, selecting keys from a list |
| 1138 |
of values. It returns a default value if any of the keys are not found at the |
| 1139 |
associated dict. |
| 1140 |
|
| 1141 |
``` |
| 1142 |
dig "user" "role" "humanName" "guest" $dict |
| 1143 |
``` |
| 1144 |
|
| 1145 |
Given a dict structured like |
| 1146 |
``` |
| 1147 |
{ |
| 1148 |
user: { |
| 1149 |
role: { |
| 1150 |
humanName: "curator" |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |
| 1154 |
``` |
| 1155 |
|
| 1156 |
the above would return `"curator"`. If the dict lacked even a `user` field, |
| 1157 |
the result would be `"guest"`. |
| 1158 |
|
| 1159 |
Dig can be very useful in cases where you'd like to avoid guard clauses, |
| 1160 |
especially since Go's template package's `and` doesn't shortcut. For instance |
| 1161 |
`and a.maybeNil a.maybeNil.iNeedThis` will always evaluate |
| 1162 |
`a.maybeNil.iNeedThis`, and panic if `a` lacks a `maybeNil` field.) |
| 1163 |
|
| 1164 |
`dig` accepts its dict argument last in order to support pipelining. |
| 1165 |
|
| 1166 |
### keys |
| 1167 |
|
| 1168 |
The `keys` function will return a `list` of all of the keys in one or more `dict` |
| 1169 |
types. Since a dictionary is _unordered_, the keys will not be in a predictable order. |
| 1170 |
They can be sorted with `sortAlpha`. |
| 1171 |
|
| 1172 |
``` |
| 1173 |
keys $myDict | sortAlpha |
| 1174 |
``` |
| 1175 |
|
| 1176 |
When supplying multiple dictionaries, the keys will be concatenated. Use the `uniq` |
| 1177 |
function along with `sortAlpha` to get a unique, sorted list of keys. |
| 1178 |
|
| 1179 |
``` |
| 1180 |
keys $myDict $myOtherDict | uniq | sortAlpha |
| 1181 |
``` |
| 1182 |
|
| 1183 |
### pick |
| 1184 |
|
| 1185 |
The `pick` function selects just the given keys out of a dictionary, creating a |
| 1186 |
new `dict`. |
| 1187 |
|
| 1188 |
``` |
| 1189 |
$new := pick $myDict "name1" "name2" |
| 1190 |
``` |
| 1191 |
|
| 1192 |
The above returns `{name1: value1, name2: value2}` |
| 1193 |
|
| 1194 |
### omit |
| 1195 |
|
| 1196 |
The `omit` function is similar to `pick`, except it returns a new `dict` with all |
| 1197 |
the keys that _do not_ match the given keys. |
| 1198 |
|
| 1199 |
``` |
| 1200 |
$new := omit $myDict "name1" "name3" |
| 1201 |
``` |
| 1202 |
|
| 1203 |
The above returns `{name2: value2}` |
| 1204 |
|
| 1205 |
### values |
| 1206 |
|
| 1207 |
The `values` function is similar to `keys`, except it returns a new `list` with |
| 1208 |
all the values of the source `dict` (only one dictionary is supported). |
| 1209 |
|
| 1210 |
``` |
| 1211 |
$vals := values $myDict |
| 1212 |
``` |
| 1213 |
|
| 1214 |
The above returns `list["value1", "value2", "value 3"]`. Note that the `values` |
| 1215 |
function gives no guarantees about the result ordering- if you care about this, |
| 1216 |
then use `sortAlpha`. |
| 1217 |
|
| 1218 |
## Type Conversion Functions |
| 1219 |
|
| 1220 |
The following type conversion functions are provided by Sprig: |
| 1221 |
|
| 1222 |
- `atoi`: Convert a string to an integer. |
| 1223 |
- `float64`: Convert to a `float64`. |
| 1224 |
- `int`: Convert to an `int` at the system's width. |
| 1225 |
- `int64`: Convert to an `int64`. |
| 1226 |
- `toDecimal`: Convert a unix octal to a `int64`. |
| 1227 |
- `toString`: Convert to a string. |
| 1228 |
- `toStrings`: Convert a list, slice, or array to a list of strings. |
| 1229 |
|
| 1230 |
Only `atoi` requires that the input be a specific type. The others will attempt |
| 1231 |
to convert from any type to the destination type. For example, `int64` can convert |
| 1232 |
floats to ints, and it can also convert strings to ints. |
| 1233 |
|
| 1234 |
### toStrings |
| 1235 |
|
| 1236 |
Given a list-like collection, produce a slice of strings. |
| 1237 |
|
| 1238 |
``` |
| 1239 |
list 1 2 3 | toStrings |
| 1240 |
``` |
| 1241 |
|
| 1242 |
The above converts `1` to `"1"`, `2` to `"2"`, and so on, and then returns |
| 1243 |
them as a list. |
| 1244 |
|
| 1245 |
### toDecimal |
| 1246 |
|
| 1247 |
Given a unix octal permission, produce a decimal. |
| 1248 |
|
| 1249 |
``` |
| 1250 |
"0777" | toDecimal |
| 1251 |
``` |
| 1252 |
|
| 1253 |
The above converts `0777` to `511` and returns the value as an int64. |
| 1254 |
|
| 1255 |
## Path and Filepath Functions |
| 1256 |
|
| 1257 |
While Sprig does not grant access to the filesystem, it does provide functions |
| 1258 |
for working with strings that follow file path conventions. |
| 1259 |
|
| 1260 |
### Paths |
| 1261 |
|
| 1262 |
Paths separated by the slash character (`/`), processed by the `path` package. |
| 1263 |
|
| 1264 |
Examples: |
| 1265 |
|
| 1266 |
* The [Linux](https://en.wikipedia.org/wiki/Linux) and |
| 1267 |
[MacOS](https://en.wikipedia.org/wiki/MacOS) |
| 1268 |
[filesystems](https://en.wikipedia.org/wiki/File_system): |
| 1269 |
`/home/user/file`, `/etc/config`; |
| 1270 |
* The path component of |
| 1271 |
[URIs](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier): |
| 1272 |
`https://example.com/some/content/`, `ftp://example.com/file/`. |
| 1273 |
|
| 1274 |
#### base |
| 1275 |
|
| 1276 |
Return the last element of a path. |
| 1277 |
|
| 1278 |
``` |
| 1279 |
base "foo/bar/baz" |
| 1280 |
``` |
| 1281 |
|
| 1282 |
The above prints "baz". |
| 1283 |
|
| 1284 |
#### dir |
| 1285 |
|
| 1286 |
Return the directory, stripping the last part of the path. So `dir "foo/bar/baz"` |
| 1287 |
returns `foo/bar`. |
| 1288 |
|
| 1289 |
#### clean |
| 1290 |
|
| 1291 |
Clean up a path. |
| 1292 |
|
| 1293 |
``` |
| 1294 |
clean "foo/bar/../baz" |
| 1295 |
``` |
| 1296 |
|
| 1297 |
The above resolves the `..` and returns `foo/baz`. |
| 1298 |
|
| 1299 |
#### ext |
| 1300 |
|
| 1301 |
Return the file extension. |
| 1302 |
|
| 1303 |
``` |
| 1304 |
ext "foo.bar" |
| 1305 |
``` |
| 1306 |
|
| 1307 |
The above returns `.bar`. |
| 1308 |
|
| 1309 |
#### isAbs |
| 1310 |
|
| 1311 |
To check whether a path is absolute, use `isAbs`. |
| 1312 |
|
| 1313 |
### Filepaths |
| 1314 |
|
| 1315 |
Paths separated by the `os.PathSeparator` variable, processed by the `path/filepath` package. |
| 1316 |
|
| 1317 |
These are the recommended functions to use when parsing paths of local filesystems, usually when dealing with local files, directories, etc. |
| 1318 |
|
| 1319 |
Examples: |
| 1320 |
|
| 1321 |
* Running on Linux or MacOS the filesystem path is separated by the slash character (`/`): |
| 1322 |
`/home/user/file`, `/etc/config`; |
| 1323 |
* Running on [Windows](https://en.wikipedia.org/wiki/Microsoft_Windows) |
| 1324 |
the filesystem path is separated by the backslash character (`\`): |
| 1325 |
`C:\Users\Username\`, `C:\Program Files\Application\`; |
| 1326 |
|
| 1327 |
#### osBase |
| 1328 |
|
| 1329 |
Return the last element of a filepath. |
| 1330 |
|
| 1331 |
``` |
| 1332 |
osBase "/foo/bar/baz" |
| 1333 |
osBase "C:\\foo\\bar\\baz" |
| 1334 |
``` |
| 1335 |
|
| 1336 |
The above prints "baz" on Linux and Windows, respectively. |
| 1337 |
|
| 1338 |
#### osDir |
| 1339 |
|
| 1340 |
Return the directory, stripping the last part of the path. So `osDir "/foo/bar/baz"` |
| 1341 |
returns `/foo/bar` on Linux, and `osDir "C:\\foo\\bar\\baz"` |
| 1342 |
returns `C:\\foo\\bar` on Windows. |
| 1343 |
|
| 1344 |
#### osClean |
| 1345 |
|
| 1346 |
Clean up a path. |
| 1347 |
|
| 1348 |
``` |
| 1349 |
osClean "/foo/bar/../baz" |
| 1350 |
osClean "C:\\foo\\bar\\..\\baz" |
| 1351 |
``` |
| 1352 |
|
| 1353 |
The above resolves the `..` and returns `foo/baz` on Linux and `C:\\foo\\baz` on Windows. |
| 1354 |
|
| 1355 |
#### osExt |
| 1356 |
|
| 1357 |
Return the file extension. |
| 1358 |
|
| 1359 |
``` |
| 1360 |
osExt "/foo.bar" |
| 1361 |
osExt "C:\\foo.bar" |
| 1362 |
``` |
| 1363 |
|
| 1364 |
The above returns `.bar` on Linux and Windows, respectively. |
| 1365 |
|
| 1366 |
#### osIsAbs |
| 1367 |
|
| 1368 |
To check whether a file path is absolute, use `osIsAbs`. |
| 1369 |
|
| 1370 |
## Flow Control Functions |
| 1371 |
|
| 1372 |
### fail |
| 1373 |
|
| 1374 |
Unconditionally returns an empty `string` and an `error` with the specified |
| 1375 |
text. This is useful in scenarios where other conditionals have determined that |
| 1376 |
template rendering should fail. |
| 1377 |
|
| 1378 |
``` |
| 1379 |
fail "Please accept the end user license agreement" |
| 1380 |
``` |
| 1381 |
|
| 1382 |
## Reflection Functions |
| 1383 |
|
| 1384 |
Sprig provides rudimentary reflection tools. These help advanced template |
| 1385 |
developers understand the underlying Go type information for a particular value. |
| 1386 |
|
| 1387 |
Go has several primitive _kinds_, like `string`, `slice`, `int64`, and `bool`. |
| 1388 |
|
| 1389 |
Go has an open _type_ system that allows developers to create their own types. |
| 1390 |
|
| 1391 |
Sprig provides a set of functions for each. |
| 1392 |
|
| 1393 |
### Kind Functions |
| 1394 |
|
| 1395 |
There are two Kind functions: `kindOf` returns the kind of an object. |
| 1396 |
|
| 1397 |
``` |
| 1398 |
kindOf "hello" |
| 1399 |
``` |
| 1400 |
|
| 1401 |
The above would return `string`. For simple tests (like in `if` blocks), the |
| 1402 |
`kindIs` function will let you verify that a value is a particular kind: |
| 1403 |
|
| 1404 |
``` |
| 1405 |
kindIs "int" 123 |
| 1406 |
``` |
| 1407 |
|
| 1408 |
The above will return `true` |
| 1409 |
|
| 1410 |
### Type Functions |
| 1411 |
|
| 1412 |
Types are slightly harder to work with, so there are three different functions: |
| 1413 |
|
| 1414 |
- `typeOf` returns the underlying type of a value: `typeOf $foo` |
| 1415 |
- `typeIs` is like `kindIs`, but for types: `typeIs "*io.Buffer" $myVal` |
| 1416 |
- `typeIsLike` works as `typeIs`, except that it also dereferences pointers. |
| 1417 |
|
| 1418 |
**Note:** None of these can test whether or not something implements a given |
| 1419 |
interface, since doing so would require compiling the interface in ahead of time. |
| 1420 |
|
| 1421 |
### deepEqual |
| 1422 |
|
| 1423 |
`deepEqual` returns true if two values are ["deeply equal"](https://golang.org/pkg/reflect/#DeepEqual) |
| 1424 |
|
| 1425 |
Works for non-primitive types as well (compared to the built-in `eq`). |
| 1426 |
|
| 1427 |
``` |
| 1428 |
deepEqual (list 1 2 3) (list 1 2 3) |
| 1429 |
``` |
| 1430 |
|
| 1431 |
The above will return `true` |
| 1432 |
|
| 1433 |
## Cryptographic and Security Functions |
| 1434 |
|
| 1435 |
Sprig provides a couple of advanced cryptographic functions. |
| 1436 |
|
| 1437 |
### sha1sum |
| 1438 |
|
| 1439 |
The `sha1sum` function receives a string, and computes it's SHA1 digest. |
| 1440 |
|
| 1441 |
``` |
| 1442 |
sha1sum "Hello world!" |
| 1443 |
``` |
| 1444 |
|
| 1445 |
### sha256sum |
| 1446 |
|
| 1447 |
The `sha256sum` function receives a string, and computes it's SHA256 digest. |
| 1448 |
|
| 1449 |
``` |
| 1450 |
sha256sum "Hello world!" |
| 1451 |
``` |
| 1452 |
|
| 1453 |
The above will compute the SHA 256 sum in an "ASCII armored" format that is |
| 1454 |
safe to print. |
| 1455 |
|
| 1456 |
### sha512sum |
| 1457 |
|
| 1458 |
The `sha512sum` function receives a string, and computes it's SHA512 digest. |
| 1459 |
|
| 1460 |
``` |
| 1461 |
sha512sum "Hello world!" |
| 1462 |
``` |
| 1463 |
|
| 1464 |
The above will compute the SHA 512 sum in an "ASCII armored" format that is |
| 1465 |
safe to print. |
| 1466 |
|
| 1467 |
### adler32sum |
| 1468 |
|
| 1469 |
The `adler32sum` function receives a string, and computes its Adler-32 checksum. |
| 1470 |
|
| 1471 |
``` |
| 1472 |
adler32sum "Hello world!" |
| 1473 |
``` |
| 1474 |
|
| 1475 |
## URL Functions |
| 1476 |
|
| 1477 |
### urlParse |
| 1478 |
Parses string for URL and produces dict with URL parts |
| 1479 |
|
| 1480 |
``` |
| 1481 |
urlParse "http://admin:[email protected]:8080/api?list=false#anchor" |
| 1482 |
``` |
| 1483 |
|
| 1484 |
The above returns a dict, containing URL object: |
| 1485 |
```yaml |
| 1486 |
scheme: 'http' |
| 1487 |
host: 'server.com:8080' |
| 1488 |
path: '/api' |
| 1489 |
query: 'list=false' |
| 1490 |
opaque: nil |
| 1491 |
fragment: 'anchor' |
| 1492 |
userinfo: 'admin:secret' |
| 1493 |
``` |
| 1494 |
|
| 1495 |
For more info, check https://golang.org/pkg/net/url/#URL |
| 1496 |
|
| 1497 |
### urlJoin |
| 1498 |
Joins map (produced by `urlParse`) to produce URL string |
| 1499 |
|
| 1500 |
``` |
| 1501 |
urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "query" "query" "scheme" "http") |
| 1502 |
``` |
| 1503 |
|
| 1504 |
The above returns the following string: |
| 1505 |
``` |
| 1506 |
proto://host:80/path?query#fragment |
| 1507 |
``` |