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
|
# Default Functions
Sprig provides tools for setting default values for templates.
## default
To set a simple default value, use `default`:
```
default "foo" .Bar
```
In the above, if `.Bar` evaluates to a non-empty value, it will be used. But if
it is empty, `foo` will be returned instead.
The definition of "empty" depends on type:
- Numeric: 0
- String: ""
- Lists: `[]`
- Dicts: `{}`
- Boolean: `false`
- And always `nil` (aka null)
For structs, there is no definition of empty, so a struct will never return the
default.
## empty
The `empty` function returns `true` if the given value is considered empty, and
`false` otherwise. The empty values are listed in the `default` section.
```
empty .Foo
```
Note that in Go template conditionals, emptiness is calculated for you. Thus,
you rarely need `if empty .Foo`. Instead, just use `if .Foo`.
## coalesce
The `coalesce` function takes a list of values and returns the first non-empty
one.
```
coalesce 0 1 2
```
The above returns `1`.
This function is useful for scanning through multiple variables or values:
```
coalesce .name .parent.name "Matt"
```
The above will first check to see if `.name` is empty. If it is not, it will return
that value. If it _is_ empty, `coalesce` will evaluate `.parent.name` for emptiness.
Finally, if both `.name` and `.parent.name` are empty, it will return `Matt`.
## all
The `all` function takes a list of values and returns true if all values are non-empty.
```
all 0 1 2
```
The above returns `false`.
This function is useful for evaluating multiple conditions of variables or values:
```
all (eq .Request.TLS.Version 0x0304) (.Request.ProtoAtLeast 2 0) (eq .Request.Method "POST")
```
The above will check http.Request is POST with tls 1.3 and http/2.
## any
The `any` function takes a list of values and returns true if any value is non-empty.
```
any 0 1 2
```
The above returns `true`.
This function is useful for evaluating multiple conditions of variables or values:
```
any (eq .Request.Method "GET") (eq .Request.Method "POST") (eq .Request.Method "OPTIONS")
```
The above will check http.Request method is one of GET/POST/OPTIONS.
## fromJson, mustFromJson
`fromJson` decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string.
`mustFromJson` will return an error in case the JSON is invalid.
```
fromJson "{\"foo\": 55}"
```
## toJson, mustToJson
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.
`mustToJson` will return an error in case the item cannot be encoded in JSON.
```
toJson .Item
```
The above returns JSON string representation of `.Item`.
## toPrettyJson, mustToPrettyJson
The `toPrettyJson` function encodes an item into a pretty (indented) JSON string.
```
toPrettyJson .Item
```
The above returns indented JSON string representation of `.Item`.
## toRawJson, mustToRawJson
The `toRawJson` function encodes an item into JSON string with HTML characters unescaped.
```
toRawJson .Item
```
The above returns unescaped JSON string representation of `.Item`.
## ternary
The `ternary` function takes two values, and a test value. If the test value is
true, the first value will be returned. If the test value is empty, the second
value will be returned. This is similar to the c ternary operator.
### true test value
```
ternary "foo" "bar" true
```
or
```
true | ternary "foo" "bar"
```
The above returns `"foo"`.
### false test value
```
ternary "foo" "bar" false
```
or
```
false | ternary "foo" "bar"
```
The above returns `"bar"`.
|