# Using variable in range for data templates

**URL:** <https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374>\
**Category:** support\
**Created:** [September 4, 2022, 8:13pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374 "2022-09-04T20:13:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [September 4, 2022, 8:13pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/1 "2022-09-04T20:13:42Z")

</div>

Hello!  
I’m trying to compose a range statement with a variable in it to be “dynamic” if the source JSON it has its information from gets updated.

The JSON files are named like this “`firstlastname.json`” and I have no trouble accessing / working with them from  
`{{ range $.Site.Data.players.firstlastname.information }}`

I read [this](https://discourse.gohugo.io/t/how-to-use-variable-in-a-range-statement/971) and my code now looks like this:

```auto
{{ $myjsonfile := .firstlastname }}
{{ range (index $.Site.Data.players $myjsonfile).information }}
    {{ .fullname }}
{{ end }}

```

Output for $myjsonfile is: `.firstlastname` (the correct JSON file name for one of the players).

I have no error or other output from this range I could work with. Any hints?

Cheers! 🙂

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 5, 2022, 2:40pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/2 "2022-09-05T14:40:32Z")

</div>

Please provide example of data file.

---

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [September 5, 2022, 2:51pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/3 "2022-09-05T14:51:52Z")

</div>

You mean of the JSON? Sure, looks like this:

```auto
{
  "information": [
    {
      "fullname": "Full Name",
      "numberofgames": 6,
      "pointstotal": 8,
      "winstotal": 2,
      "drawstotal": 2,
      "lossestotal": 2,
      "pointsaverage": 1.3333333333333333,
      "winpercentage": 33,
      "winchance": "low"
    }
  ],
  "games": [
    {
      "date": "2018-03-18T00:00:00+00:00",
      "fixtureID": -,
      "timestamp": -,
      "leagueid": 78,
      "hometeam": "-",
      "hometeamgoal": 2,
      "awayteam": "-",
      "awayteamgoal": 0,
      "resultforplayer": "win"
    },
    {
      "date": "2018-02-25T00:00:00+00:00",
      "fixtureID": -,
      "timestamp": -,
      "leagueid": 78,
      "hometeam": "-",
      "hometeamgoal": 1,
      "awayteam": "-",
      "awayteamgoal": 2,
      "resultforplayer": "win"
    },
]
}

```

But as I said, it works fine if I manually enter the filename in the range (`range $.Site.Data.players.firstlastname.information)`, so I _THINK_ this might not be the problem, but I put the range with variable together wrongly.

Interestingly I have no error while implementing the `range (index $.Site.Data.players $myjsonfile).information`, but nothing in between the `range` and the `end` is actually loaded on the page, even if its plain text.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 5, 2022, 2:54pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/4 "2022-09-05T14:54:08Z")

</div>

Why is “information” an array instead of an object?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [September 5, 2022, 3:03pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/5 "2022-09-05T15:03:57Z")

</div>

This works as expected:

```auto
{{ $myjsonfile := "firstlastname" }}
{{ range (index $.Site.Data.players $myjsonfile).information }}
  {{ .fullname }}
{{ end }}

```

Try it:

```bash
git clone --single-branch -b hugo-forum-topic-40374 https://github.com/jmooring/hugo-testing hugo-forum-topic-40374
cd hugo-forum-topic-40374
hugo server

```

---

<div class="post-metadata">

**Author:** ![xdk88](https://avatars.discourse-cdn.com/v4/letter/x/91b2a8/32.png) [@xdk88](https://discourse.gohugo.io/u/xdk88)\
**Post date:** [September 5, 2022, 3:34pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/6 "2022-09-05T15:34:44Z")

</div>

Thanks, that was it! Since the other topic also had the example for declaring the variable starting with a dot and it matched the general construction of the range, I was convinced I also had to declare it like that. Removed the dot and it worked as expected. Many thanks for your time! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [September 7, 2022, 3:35pm UTC](https://discourse.gohugo.io/t/using-variable-in-range-for-data-templates/40374/7 "2022-09-07T15:35:01Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
