hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

index-function.md (3180B)

    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 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [functions]
    9 menu:
   10   docs:
   11     parent: "functions"
   12 keywords: []
   13 signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"]
   14 workson: []
   15 hugoversion:
   16 relatedfuncs: []
   17 deprecated: false
   18 aliases: [/functions/index/]
   19 needsexample: true
   20 ---
   21 
   22 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.:
   23 
   24 ```go-text-template
   25 {{ $slice := slice "a" "b" "c" }}
   26 {{ index $slice 1 }} => b
   27 {{ $map := dict "a" 100 "b" 200 }}
   28 {{ index $map "b" }} => 200
   29 ```
   30 
   31 The function takes multiple indices as arguments, and this can be used to get nested values, e.g.:
   32 
   33 ```go-text-template
   34 {{ $map := dict "a" 100 "b" 200 "c" (slice 10 20 30) }}
   35 {{ index $map "c" 1 }} => 20
   36 {{ $map := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }}
   37 {{ index $map "c" "e" }} => 20
   38 ```
   39 
   40 You may write multiple indices as a slice:
   41 
   42 ```go-text-template
   43 {{ $map := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }}
   44 {{ $slice := slice "c" "e" }}
   45 {{ index $map $slice }} => 20
   46 ```
   47 
   48 ## Example: Load Data from a Path Based on Front Matter Params
   49 
   50 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:
   51 
   52 ```
   53 .
   54 └── data
   55     └── locations
   56         ├── abilene.toml
   57         ├── chicago.toml
   58         ├── oslo.toml
   59         └── provo.toml
   60 ```
   61 
   62 Here is an example:
   63 
   64 {{< code-toggle file="data/locations/oslo" >}}
   65 website = "https://www.oslo.kommune.no"
   66 pop_city = 658390
   67 pop_metro = 1717900
   68 {{< /code-toggle >}}
   69 
   70 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/`:
   71 
   72 ```
   73 title = "My Norwegian Vacation"
   74 location = "oslo"
   75 ```
   76 
   77 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.
   78 
   79 This is where the `index` function is needed. `index` takes 2 parameters in this use case:
   80 
   81 1. The node path
   82 2. A string corresponding to the desired data; e.g.&mdash;
   83 
   84 ```
   85 {{ index .Site.Data.locations “oslo” }}
   86 ```
   87 
   88 The variable for `.Params.location` is a string and can therefore replace `oslo` in the example above:
   89 
   90 ```
   91 {{ index .Site.Data.locations .Params.location }}
   92 => map[website:https://www.oslo.kommune.no pop_city:658390 pop_metro:1717900]
   93 ```
   94 
   95 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 (`.`):
   96 
   97 ```
   98 {{ (index .Site.Data.locations .Params.location).pop_city }}
   99 => 658390
  100 ```