# Range through translated data file if the file name and frontmatter variable are the same

**URL:** <https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586>\
**Category:** support\
**Tags:** i18n\
**Created:** [July 19, 2022, 2:19pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586 "2022-07-19T14:19:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 19, 2022, 2:19pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/1 "2022-07-19T14:19:43Z")

</div>

Is it possible in a shortcode to compare if a data file is the same as a frontmatter variable and then range the data of each respective file based on this parameter? For example, file name is `t2_01`, `t1_02`, etc. with frontmatter variables of `code: t2_01` etc. Currently, I can slice through the frontmatter variables and assign the layouts manually, but I dread how long this part will become if I add hundreds of files.

```auto
{{ with index site.Data.country.capital site.Language.Lang }}
<table>
  <thead>
  <tr>
  </tr>
  </thead>
  <tbody>
    {{ $file := slice }}
    {{ if eq $.Page.Params.code "t2_01"}}
    {{ $file = .t2_01 }}
    {{ end }}
    {{- range $file }}
   <tr>
    <td>{{ .Country }}</td>
    <td>{{ .Target }}</td>
    <td>{{ .Actual }}</td>
    <td>{{ .Percent }}</td>
   </tr>
   {{- end }}
  </tbody>
  </table>
{{- end }}

```

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 24, 2022, 3:02pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/2 "2022-07-24T15:02:44Z")

</div>

If anyone has an idea, kindly share.

---

<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:** [July 24, 2022, 3:38pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/3 "2022-07-24T15:38:31Z")

</div>

Your question is unclear, but remember that the `index` function accepts multiple keys.

{{ index site.Data.foo “bar” “baz” “quz” }}

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 24, 2022, 4:59pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/4 "2022-07-24T16:59:45Z")

</div>

I will try to explain in points format what I am doing here.

1. I have data (in JSON format) that I want to convert to tables. The structure of the data folder is as follows (languages are examples).

```auto
├───data
│ └───country
│ └───capital
│ ├───en
│ │ t2_01.json
│ │
│ └───de
│ t2_01.json

```

and the data as follows

```auto
[
    {
        "Country": "Foo",
        "Target": "346,088,720",
        "Actual": "205,203,689",
        "Percent": "59.3"
    }
]

```

1. At the same time, I am assigning a frontmatter variable per [multipage](https://discourse.gohugo.io/t/splitting-an-article-into-multiple-pages/29161/3) that corresponds to the data file name (I am yet unsure if to do something like `t2_01: true` or `code: t2_01` ).
2. In my table shortcode (see first post), I want to use one shortcode for all the tables since they share the same table head (translated with `i18n`) . My question is, therefore, if it is possible to match the file name and its corresponding front matter variable so that only the correct data is shown in each respective multi page, depending on the frontmatter variable, for both the original and translated pages?
3. Since I am translating the data, my wish is for the shortcode for the table I shared in the first to be in every page, but the data to be shown per page be only that which corresponds to the filtered condition.
4. I aim to have hundreds of data files, so I wanted to use only one shortcode that covers everything, rather than hundreds of different ones.
5. Please let me know if you need further clarification.

---

<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:** [July 24, 2022, 5:13pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/5 "2022-07-24T17:13:12Z")

</div>

Do you understand what I wrote about the index function?

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 24, 2022, 5:30pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/6 "2022-07-24T17:30:19Z")

</div>

No, unfortunately. This is the first time I am delving into data with Hugo. (I want to avoid using inline HTML)

---

<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:** [July 24, 2022, 5:37pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/7 "2022-07-24T17:37:21Z")

</div>

structure

```nohighlight
data/
└── foo/
    └── bar/
        └── baz/
            └── quz.json

```

template (these return the same object)

```auto
{{ index site.Data.foo.bar.baz.quz }}
{{ index site.Data.foo.bar.baz "quz" }}
{{ index site.Data.foo.bar "baz" "quz" }}
{{ index site.Data.foo "bar" "baz" "quz" }}
{{ index site.Data "foo" "bar" "baz" "quz" }}

```

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [July 24, 2022, 6:21pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/8 "2022-07-24T18:21:46Z")

</div>

This worked–

```auto
{{ $file := index site.Data.country.capital site.Language.Lang ($.Page.Params.code }}
    {{- range $file }}
<!--- data here in table format -->
{{ end }}

```

---

<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:** [July 26, 2022, 6:21pm UTC](https://discourse.gohugo.io/t/range-through-translated-data-file-if-the-file-name-and-frontmatter-variable-are-the-same/39586/9 "2022-07-26T18:21:52Z")

</div>

Closed on July 26, 2022, 6:21pm UTC

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