hugo/content/en/functions/index-function.md
Bjørn Erik Pedersen efa74c5c6e Squashed 'docs/' changes from 9be494de3..ac2c4a487
ac2c4a487 Update documentation for Ugly URLs (#1082)
88bdec17a Change 072.0 to 0.72.0 in release post's description
2aa7d7818 Update rss.md (#1104)
c80677aeb Update quick-start.md (#1076)
d04196bbd Minor spelling and capitalization fixes
837d2feba Fixed spelling mistake
67dc78e12 Update installing.md
ce280c5d6 Update relurl.md
bb4d0e703 Capitalization and Redirecting URL fixes
e1fecada0 Update partials.md
1d99bb182 Typos and whitespacing issues fixed
b20dba125 actually fix index function link this time
f47d6f1e3 Fixing typos, whitespace issues and links
dc82309b9 fix link to the index function
1eab0cbea add missing word (#1130)
9c3ee62ae more fixes
e9bc5880a whitespace, typos and HTTPS fixes
93b806493 Add missing word to Module section
80ced9062 Display image on page bundles page.
727029b0a Update index.md
51fc48e4d Release 0.72.0
1ff68ac3b releaser: Add release notes to /docs for release of 0.72.0
f74a25b92 common/maps: Add Scratch.Values
2fd83db96 Add redirect support to the server
bdfccf9f4 Fix typo in install instructions
e12737ea6 Create SUPPORT.md

git-subtree-dir: docs
git-subtree-split: ac2c4a4871e90ddfb180f23704ce7ec9023529ca
2020-06-16 14:18:51 +02:00

93 lines
3 KiB
Markdown

---
title: index
linktitle: index
description: Looks up the index(es) or key(s) of the data structure passed into it.
godocref: https://golang.org/pkg/text/template/#hdr-Functions
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [functions]
menu:
docs:
parent: "functions"
keywords: []
signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"]
workson: []
hugoversion:
relatedfuncs: []
deprecated: false
aliases: [/functions/index/]
needsexample: true
---
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.:
```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
```
## Example: Load Data from a Path Based on Front Matter Params
Assume you want to add a `location = ""` field to your front matter for every article written in `content/vacations/`. You want to use this field to populate information about the location at the bottom of the article in your `single.html` template. You also have a directory in `data/locations/` that looks like the following:
```
.
└── data
└── locations
├── abilene.toml
├── chicago.toml
├── oslo.toml
└── provo.toml
```
Here is an example of the data inside `data/locations/oslo.toml`:
```
website = "https://www.oslo.kommune.no"
pop_city = 658390
pop_metro = 1717900
```
The example we will use will be an article on Oslo, whose front matter should be set to exactly the same name as the corresponding file name in `data/locations/`:
```
title = "My Norwegian Vacation"
location = "oslo"
```
The content of `oslo.toml` can be accessed from your template using the following node path: `.Site.Data.locations.oslo`. However, the specific file you need is going to change according to the front matter.
This is where the `index` function is needed. `index` takes 2 parameters in this use case:
1. The node path
2. A string corresponding to the desired data; e.g.—
```
{{ index .Site.Data.locations “oslo” }}
```
The variable for `.Params.location` is a string and can therefore replace `oslo` in the example above:
```
{{ index .Site.Data.locations .Params.location }}
=> map[website:https://www.oslo.kommune.no pop_city:658390 pop_metro:1717900]
```
Now the call will return the specific file according to the location specified in the content's front matter, but you will likely want to write specific properties to the template. You can do this by continuing down the node path via dot notation (`.`):
```
{{ (index .Site.Data.locations .Params.location).pop_city }}
=> 658390
```