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