Getting specicific element from a json data template

With a States.json file in my data folder for example

{
 "AL": {
   "name": "Alabama",
   "capital": "Montgomery",
   "lat": "32.361538",
   "long": "-86.279118"
 },
 "AK": {
   "name": "Alaska",
   "capital": "Juneau",
   "lat": "58.301935",
   "long": "-134.419740"
 }
}

It’s easy enough to get say AL by writing $.Site.Data.states.AL.capital

However, what I would really like to do is define a state in the frontmatter of content file, for instance, similar to how the front matter of the _index.md file in the hugodocs looks:

title: "A Fast and Flexible Website Generator"
date: 2017-03-02T12:00:00-05:00
features:
  - heading: Blistering Speed
    image_path: /images/icon-fast.svg
    state: AK
    tagline: What's modern about waiting for your site to build?
    copy: Hugo is the fastest tool of its kind. At <1 ms per page, the average site builds in less than a second.

  - heading: Robust Content Management
    image_path: /images/icon-content-management.svg
    state: AL
    tagline: Flexibility rules. Hugo is a content strategist's dream.
    copy: Hugo supports unlimited content types, taxonomies, menus, dynamic API-driven content, and more, all without plugins.

Except, instead of having an image_path for each feature, I might have a state like AK. So that when I loop through the features as is also done in the hugo repo, I could in each case - access the capital of a state defined in the front matter. It is here where I am unsure of what the syntax would look like. Naively I expect something along the lines of:

{{ $features := .Params.features }}
  {{ range $i, $e := $features  }}
    {{ $features_count := $e | len }}
            <h1>{{$.Site.Data.states.state.capital}}</h1>
{{end}}

But this hasn’t worked when i’ve tried it - so i’m wondering what would be the correct way of doing this? Thanks!

You need to use the index func.

Thank you, don’t know how I missed that :confused:

1 Like