diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2022-12-30 10:03:39 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2023-01-21 11:09:38 -0500 |
| commit | cfcada535690dd4a2a91503887f55c0b699b4006 (patch) | |
| tree | 2fca63abd5af068a82b0a4c7f47d89349d0c64f4 /docs/content/functions/coll.md | |
| parent | 86e080458f789525e7105f5340499ff3eb8c9c04 (diff) | |
New coll.Index function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'docs/content/functions/coll.md')
| -rw-r--r-- | docs/content/functions/coll.md | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/docs/content/functions/coll.md b/docs/content/functions/coll.md index 55f2b9cf..b78fe4ca 100644 --- a/docs/content/functions/coll.md +++ b/docs/content/functions/coll.md @@ -165,6 +165,48 @@ $ gomplate -i '{{ $o := data.JSON (getenv "DATA") -}} THERE IS NO FOO ``` +## `coll.Index` + +Returns the result of indexing the given map, slice, or array by the given +key or index. This is similar to the built-in `index` function, but the +arguments are ordered differently for pipeline compatibility. Also this +function is more strict, and will return an error when trying to access a +non-existent map key. + +Multiple indexes may be given, for nested indexing. + +### Usage + +```go +coll.Index indexes... in +``` +```go +in | coll.Index indexes... +``` + +### Arguments + +| name | description | +|------|-------------| +| `indexes...` | _(required)_ The key or index | +| `in` | _(required)_ The map, slice, or array to index | + +### Examples + +```console +$ gomplate -i '{{ coll.Index "foo" (dict "foo" "bar") }}' +bar +``` +```console +$ gomplate -i '{{ $m := json `{"foo": {"bar": "baz"}}` -}} + {{ coll.Index "foo" "bar" $m }}' +baz +``` +```console +$ gomplate -i '{{ coll.Slice "foo" "bar" "baz" | coll.Index 1 }}' +bar +``` + ## `coll.JSONPath` **Alias:** `jsonpath` |
