I have a JSOn file with some words and definitions.
I want to call a certain section of it into my website.
So for example if I write
{{ Dictionary word=“Apple”}}
It would go through the JSON file, find Apple and display its definition.
{{ range $.Site.Data.Dictionary}}
{{ if eq $word .word }}
{{ .word} - {{ .definition}}
<br>
<br>
{{end}}
{{end}}
This is what I tried but said the word is an undefined variable.
Edit: I did it. Yay. Using the index function.
Is the $word
variable defined outside the range? It’s not included here so it’s hard to understand what might be causing the problem. undefined
errors, however, are classically JavaScript. If it were a Hugo error it would likely be a nil
error so you may want to run your JSON file through a validator to ensure it’s well formed or try porting it over to TOML or YAML instead.
Then use template debugging to pinpoint any template issues:
{{ printf "%#v" $word }}
I am new to Hugo. How do I define the variable or parameter?
Doesn’t feel like you’re calling this shortcode correctly. Perhaps {{< dictionary word="Apple" >}}
. Per @jhabdas’s comment, can you possibly share your source and the .json
file?
Guys I did it I think using the index function. Sorry for all the trouble I caused. It works I just need to wait and see if any problems arise.
{{ $word := .Get “word” }}
{{ $data := .Site.Data.dictionary }}
{{ (index $data .Params.word).word }} - {{ (index $data .Params.word).definition }}
1 Like