| 1 |
# Default Functions |
| 2 |
|
| 3 |
Sprig provides tools for setting default values for templates. |
| 4 |
|
| 5 |
## default |
| 6 |
|
| 7 |
To set a simple default value, use `default`: |
| 8 |
|
| 9 |
``` |
| 10 |
default "foo" .Bar |
| 11 |
``` |
| 12 |
|
| 13 |
In the above, if `.Bar` evaluates to a non-empty value, it will be used. But if |
| 14 |
it is empty, `foo` will be returned instead. |
| 15 |
|
| 16 |
The definition of "empty" depends on type: |
| 17 |
|
| 18 |
- Numeric: 0 |
| 19 |
- String: "" |
| 20 |
- Lists: `[]` |
| 21 |
- Dicts: `{}` |
| 22 |
- Boolean: `false` |
| 23 |
- And always `nil` (aka null) |
| 24 |
|
| 25 |
For structs, there is no definition of empty, so a struct will never return the |
| 26 |
default. |
| 27 |
|
| 28 |
## empty |
| 29 |
|
| 30 |
The `empty` function returns `true` if the given value is considered empty, and |
| 31 |
`false` otherwise. The empty values are listed in the `default` section. |
| 32 |
|
| 33 |
``` |
| 34 |
empty .Foo |
| 35 |
``` |
| 36 |
|
| 37 |
Note that in Go template conditionals, emptiness is calculated for you. Thus, |
| 38 |
you rarely need `if empty .Foo`. Instead, just use `if .Foo`. |
| 39 |
|
| 40 |
## coalesce |
| 41 |
|
| 42 |
The `coalesce` function takes a list of values and returns the first non-empty |
| 43 |
one. |
| 44 |
|
| 45 |
``` |
| 46 |
coalesce 0 1 2 |
| 47 |
``` |
| 48 |
|
| 49 |
The above returns `1`. |
| 50 |
|
| 51 |
This function is useful for scanning through multiple variables or values: |
| 52 |
|
| 53 |
``` |
| 54 |
coalesce .name .parent.name "Matt" |
| 55 |
``` |
| 56 |
|
| 57 |
The above will first check to see if `.name` is empty. If it is not, it will return |
| 58 |
that value. If it _is_ empty, `coalesce` will evaluate `.parent.name` for emptiness. |
| 59 |
Finally, if both `.name` and `.parent.name` are empty, it will return `Matt`. |
| 60 |
|
| 61 |
## all |
| 62 |
|
| 63 |
The `all` function takes a list of values and returns true if all values are non-empty. |
| 64 |
|
| 65 |
``` |
| 66 |
all 0 1 2 |
| 67 |
``` |
| 68 |
|
| 69 |
The above returns `false`. |
| 70 |
|
| 71 |
This function is useful for evaluating multiple conditions of variables or values: |
| 72 |
|
| 73 |
``` |
| 74 |
all (eq .Request.TLS.Version 0x0304) (.Request.ProtoAtLeast 2 0) (eq .Request.Method "POST") |
| 75 |
``` |
| 76 |
|
| 77 |
The above will check http.Request is POST with tls 1.3 and http/2. |
| 78 |
|
| 79 |
## any |
| 80 |
|
| 81 |
The `any` function takes a list of values and returns true if any value is non-empty. |
| 82 |
|
| 83 |
``` |
| 84 |
any 0 1 2 |
| 85 |
``` |
| 86 |
|
| 87 |
The above returns `true`. |
| 88 |
|
| 89 |
This function is useful for evaluating multiple conditions of variables or values: |
| 90 |
|
| 91 |
``` |
| 92 |
any (eq .Request.Method "GET") (eq .Request.Method "POST") (eq .Request.Method "OPTIONS") |
| 93 |
``` |
| 94 |
|
| 95 |
The above will check http.Request method is one of GET/POST/OPTIONS. |
| 96 |
|
| 97 |
## fromJson, mustFromJson |
| 98 |
|
| 99 |
`fromJson` decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string. |
| 100 |
`mustFromJson` will return an error in case the JSON is invalid. |
| 101 |
|
| 102 |
``` |
| 103 |
fromJson "{\"foo\": 55}" |
| 104 |
``` |
| 105 |
|
| 106 |
## toJson, mustToJson |
| 107 |
|
| 108 |
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. |
| 109 |
`mustToJson` will return an error in case the item cannot be encoded in JSON. |
| 110 |
|
| 111 |
``` |
| 112 |
toJson .Item |
| 113 |
``` |
| 114 |
|
| 115 |
The above returns JSON string representation of `.Item`. |
| 116 |
|
| 117 |
## toPrettyJson, mustToPrettyJson |
| 118 |
|
| 119 |
The `toPrettyJson` function encodes an item into a pretty (indented) JSON string. |
| 120 |
|
| 121 |
``` |
| 122 |
toPrettyJson .Item |
| 123 |
``` |
| 124 |
|
| 125 |
The above returns indented JSON string representation of `.Item`. |
| 126 |
|
| 127 |
## toRawJson, mustToRawJson |
| 128 |
|
| 129 |
The `toRawJson` function encodes an item into JSON string with HTML characters unescaped. |
| 130 |
|
| 131 |
``` |
| 132 |
toRawJson .Item |
| 133 |
``` |
| 134 |
|
| 135 |
The above returns unescaped JSON string representation of `.Item`. |
| 136 |
|
| 137 |
## ternary |
| 138 |
|
| 139 |
The `ternary` function takes two values, and a test value. If the test value is |
| 140 |
true, the first value will be returned. If the test value is empty, the second |
| 141 |
value will be returned. This is similar to the c ternary operator. |
| 142 |
|
| 143 |
### true test value |
| 144 |
|
| 145 |
``` |
| 146 |
ternary "foo" "bar" true |
| 147 |
``` |
| 148 |
|
| 149 |
or |
| 150 |
|
| 151 |
``` |
| 152 |
true | ternary "foo" "bar" |
| 153 |
``` |
| 154 |
|
| 155 |
The above returns `"foo"`. |
| 156 |
|
| 157 |
### false test value |
| 158 |
|
| 159 |
``` |
| 160 |
ternary "foo" "bar" false |
| 161 |
``` |
| 162 |
|
| 163 |
or |
| 164 |
|
| 165 |
``` |
| 166 |
false | ternary "foo" "bar" |
| 167 |
``` |
| 168 |
|
| 169 |
The above returns `"bar"`. |