mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Merge commit 'efc0b1bb6c6564f54d596467dbc6a18cb206954e'
This commit is contained in:
@@ -11,7 +11,7 @@ menu:
|
||||
docs:
|
||||
parent: "functions"
|
||||
keywords: []
|
||||
signature: ["index COLLECTION INDEX", "index COLLECTION KEY"]
|
||||
signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"]
|
||||
workson: []
|
||||
hugoversion:
|
||||
relatedfuncs: []
|
||||
@@ -20,13 +20,25 @@ aliases: [/functions/index/]
|
||||
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
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: print
|
||||
linktitle: print
|
||||
description: Prints the default representation of the given argument using the standard `fmt.Print` function.
|
||||
description: Prints the default representation of the given arguments using the standard `fmt.Print` function.
|
||||
godocref: https://golang.org/pkg/fmt/
|
||||
date: 2017-02-01
|
||||
publishdate: 2017-02-01
|
||||
@@ -22,5 +22,6 @@ See [the go doc](https://golang.org/pkg/fmt/) for additional information.
|
||||
|
||||
```
|
||||
{{ print "foo" }} → "foo"
|
||||
{{ print "foo" "bar" }} → "foobar"
|
||||
{{ print (slice 1 2 3) }} → [1 2 3]
|
||||
```
|
||||
|
Reference in New Issue
Block a user