In my data file img.toml
, I want to have something like
img = [
{ file = "...", type = "...", size = "...", }
{ file = "...", type = "...", size = "...", }
{ file = "...", type = "...", size = "...", }
]
I later in a template want to iterate over this and access each item like ....file
, ....type
, ....size
.
In another situation, I want to use a map like:
img = [
key1 -> { file = "...", type = "...", size = "...", }
key2 -> { file = "...", type = "...", size = "...", }
key3 -> { file = "...", type = "...", size = "...", }
]
Where I want to iterate over key and values and access inner properties of the items.
Hi @sirinath,
Not quite sure what is the question you are trying to ask, but looks like you are trying to create a custom data template.
Did you check the Data Templates in Hugo?
I checked them out. What is not clear to me is how to have Key-Value mapping in the toml
file. The example given in the documentation uses a list which on which range
is used. I am looking to use range
over Key-Value map or dictionary.
I am looking to implement and iterable map with key-value items which can be iterated using {{ range $elem_key, $elem_val := $.Site.Data.items }}
.
The key-value themselves are composites which I should be able to access as $elem_key.KeyProperty1
, $elem_val.ValueProperty1
, $elem_key.KeyProperty2
, $elem_val.ValueProperty2
, etc.
@sirinath it seems that you have already answered your question.
This is more of a question about data file format used in Hugo Data Templates than the specifics of Hugo Templates.
I am still not sure how to define a map/dictionary in the toml
data template. To archive something like:
img = [
key1 -> { file = "...", type = "...", size = "...", }
key2 -> { file = "...", type = "...", size = "...", }
key3 -> { file = "...", type = "...", size = "...", }
]
which can be iterated over using {{ range $elem_key, $elem_val := $.Site.Data.img }}
. What is the correct format for data like the above to be consumed in Hugo {{ range $elem_key, $elem_val := $.Site.Data.img }}
and to be consumed in Hugo $elem_key.KeyProperty1
, $elem_val.ValueProperty1
, $elem_key.KeyProperty2
, $elem_val.ValueProperty2
, etc.? https://gohugo.io/templates/introduction/#iteration gives the iteration syntax and https://gohugo.io/templates/data-templates/ gives how to define a simple list without inner properties and how to iterate over it. https://toml.io/en/v1.0.0-rc.1 does not mention much about how this is defined either and how it can be accessed from a template implementation like Hugo templates.
Also is this the way to define a list with inner properties:
img = [
{ file = "...", type = "...", size = "...", }
{ file = "...", type = "...", size = "...", }
{ file = "...", type = "...", size = "...", }
]
which can be iterated over using {{ range $elem_val := $.Site.Data.img }}
accessing inner properties using $elem_val.ValueProperty1
. Just to verify my understanding.
There are resources available online that teach how to use toml
syntax. In fact it is right there in the url you posted: TOML: English v1.0.0-rc.1
You can also use json
and yaml
files if you are more comfortable using their syntax.
toml
syntax is not a Hugo-specific question and is off-topic in this forum, so Iām closing this now.