mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-09 11:34:40 -04:00
Rework the index doc
Closes https://github.com/gohugoio/hugo/pull/3974 Closes https://github.com/gohugoio/hugo/issues/3289
This commit is contained in:
parent
6f0c67c082
commit
0403550130
@ -11,7 +11,7 @@ menu:
|
|||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: "functions"
|
||||||
keywords: []
|
keywords: []
|
||||||
signature: ["index COLLECTION INDEX", "index COLLECTION KEY"]
|
signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"]
|
||||||
workson: []
|
workson: []
|
||||||
hugoversion:
|
hugoversion:
|
||||||
relatedfuncs: []
|
relatedfuncs: []
|
||||||
@ -20,13 +20,25 @@ aliases: [/functions/index/]
|
|||||||
needsexample: true
|
needsexample: true
|
||||||
---
|
---
|
||||||
|
|
||||||
From the Godocs:
|
The `index` functions returns the result of indexing its first argument by the following arguments. Each indexed item must be a map or a slice, e.g.:
|
||||||
|
|
||||||
> Returns the result of indexing its first argument by the following arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each indexed item must be a map, slice, or array.
|
```go-text-template
|
||||||
|
{{ $slice := slice "a" "b" "c" }}
|
||||||
|
{{ index $slice 1 }} => b
|
||||||
|
{{ $map := dict "a" 100 "b" 200 }}
|
||||||
|
{{ index $map "b" }} => 200
|
||||||
|
```
|
||||||
|
|
||||||
|
The function takes multiple indices as arguments, and this can be used to get nested values, e.g.:
|
||||||
|
|
||||||
|
```go-text-template
|
||||||
|
{{ $map := dict "a" 100 "b" 200 "c" (slice 10 20 30) }}
|
||||||
|
{{ index $map "c" 1 }} => 20
|
||||||
|
{{ $map := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }}
|
||||||
|
{{ index $map "c" "e" }} => 20
|
||||||
|
```
|
||||||
|
|
||||||
In Go templates, you can't access array, slice, or map elements directly the same way you would in Go. For example, `$.Site.Data.authors[.Params.authorkey]` isn't supported syntax.
|
|
||||||
|
|
||||||
Instead, you have to use `index`, a function that handles the lookup for you.
|
|
||||||
|
|
||||||
## Example: Load Data from a Path Based on Front Matter Params
|
## Example: Load Data from a Path Based on Front Matter Params
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user