github.com/jbramsden/hugo@v0.47.1/docs/content/en/functions/index-function.md (about)

     1  ---
     2  title: index
     3  linktitle: index
     4  description: Looks up the index(es) or key(s) of the data structure passed into it.
     5  godocref: https://golang.org/pkg/text/template/#hdr-Functions
     6  date: 2017-02-01
     7  publishdate: 2017-02-01
     8  lastmod: 2017-02-01
     9  categories: [functions]
    10  menu:
    11    docs:
    12      parent: "functions"
    13  keywords: []
    14  signature: ["index COLLECTION INDEX", "index COLLECTION KEY"]
    15  workson: []
    16  hugoversion:
    17  relatedfuncs: []
    18  deprecated: false
    19  aliases: [/functions/index/]
    20  needsexample: true
    21  ---
    22  
    23  From the Godocs:
    24  
    25  > 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.
    26  
    27  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.
    28  
    29  Instead, you have to use `index`, a function that handles the lookup for you.
    30  
    31  ## Example: Load Data from a Path Based on Front Matter Params
    32  
    33  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:
    34  
    35  ```
    36  .
    37  └── data
    38      └── locations
    39          ├── abilene.toml
    40          ├── chicago.toml
    41          ├── oslo.toml
    42          └── provo.toml
    43  ```
    44  
    45  Here is an example of the data inside `data/locations/oslo.toml`:
    46  
    47  ```
    48  website = "https://www.oslo.kommune.no"
    49  pop_city = 658390
    50  pop_metro = 1717900
    51  ```
    52  
    53  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/`:
    54  
    55  ```
    56  title = "My Norwegian Vacation"
    57  location = "oslo"
    58  ```
    59  
    60  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.
    61  
    62  This is where the `index` function is needed. `index` takes 2 parameters in this use case:
    63  
    64  1. The node path
    65  2. A string corresponding to the desired data; e.g.—
    66  
    67  ```
    68  {{ index .Site.Data.locations “oslo” }}
    69  ```
    70  
    71  The variable for `.Params.location` is a string and can therefore replace `oslo` in the example above:
    72  
    73  ```
    74  {{ index .Site.Data.locations .Params.location }}
    75  => map[website:https://www.oslo.kommune.no pop_city:658390 pop_metro:1717900]
    76  ```
    77  
    78  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 (`.`):
    79  
    80  ```
    81  {{ (index .Site.Data.locations .Params.location).pop_city }}
    82  => 658390
    83  ```
    84