Encountered a fascinating yet challenging scenario while working with data from an API that highlights the limitations of using a templating language like Handlebars. (Helpers, Helpers everywhere!)
Is there a elegant solution for handling the following situation? Essentially, if a JSON object has only one sibling, it is not wrapped with a key. However, if it returns multiple siblings, they are wrapped with an index key.
For instance, the first example below shows an object with one ingredient, while the second example returns two ingredients, wrapped with an index key.
I am unable to modify the data format, so I need to find a way to handle it effectively.
Although I believe using a helper is necessary, I am hoping there is a clever trick to manage this situation to cater to different data returns. Ideally, I would like to achieve this using pure Handlebars (without relying on Ember, etc.).
If anyone has any suggestions or solutions, I would greatly appreciate it!
Scenario 1:
"food":{
"ingredient":{
"name":"Cucumber",
"weight":"5",
"cost":"1",
}
}
Scenario 2:
"food":{
"ingredient":{
"1":{
"name":"Cheese",
"weight":"10",
"cost":"2"
},
"2":{
"name":"Tomato",
"weight":"20",
"cost":"0.5"
}
}
}