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

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.

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

If anyone has an idea, kindly share.

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

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

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).
│   └───country
│       └───capital
│           ├───en
│           │       t2_01.json
│           │
│           └───de
│                   t2_01.json

and the data as follows

        "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 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.

Do you understand what I wrote about the index function?

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


└── foo/
    └── bar/
        └── baz/
            └── quz.json

template (these return the same object)

{{ 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" }}

This worked–

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

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