Range index .Site.Data access to .Params

Hey
I want to have acces to data from .json

this is goodmorning.json

[{
    "lang": "en",
    "word": "Good morning"
}, {
    "lang": "de",
    "word": "Guten Morgen"
}]

This is goodmorning.md:

---
title: "goodmorning"
file: "goodmorning"
---

in content.html this code is working

  {{ range index .Site.Data .Title }}
  {{ .c }} - {{ .word }} 
  <br>
{{ end }}

BUT
I would like to have goodmorning.md like (look that I have space between good and morning) so i want to acces .json via file Param:

---
title: "Good morning"
file: "goodmorning"
---

and I cant acces data from json if I have:

  {{ range index .Site.Data .Params.file }}
  {{ .c }} - {{ .word }} 
  <br>
{{ end }}

I tried a lot of various solution but cant find a solution, can anybody can help me? I cant just type
{{ range index .Site.Data "goodmorning" }} becouse i want to create hundreds of .json and .md files

I’m not sure I understand.

  • Are you creating files in the data directory for every page?
  • If yes, is each data file only used with one page?

Yes to all questions.

–
I created goodmorning.md to let Hugo know that should read the goodmorning.json and display data from json

Would you consider placing the JSON files adjacent to each page instead?

content/
β”œβ”€β”€ phrases/
β”‚   β”œβ”€β”€ good-morning/
β”‚   β”‚   β”œβ”€β”€ index.md
β”‚   β”‚   └── translations.json
β”‚   └── one-beer-please/
β”‚       β”œβ”€β”€ index.md
β”‚       └── translations.json
└── _index.md

This structure is less fragile and easier to manage.

Thanks for reply! Always when I search this forum or github your answer are very helpful!

Honestly I didn’t think about this, I always created it in Hugo using the standard structure.

Could you tell me why it is better? I can’t figure it out how to display those data from .json and .md, I tried a lot of various examples

Take this for a spin:

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

The important bits are:

  • layouts/phrases/single.html
  • layouts/partials/render-translation-table.html

With content and resources/assets in the same directory, there’s no need to link them together. It’s easier to manage everything, and less prone to error.

1 Like