Handlebars - Element Reference
Con rispostaHi
How can I reference an element in an outer iteration if an element with the same name already exists in the inner iteration?
Here's a sample JSON payload:
The element I'm referring to in this example is the "id" element. It exists in the "data" array and in the "location" array.
{
"data": [
{
"id": 35435,
"company": {
"name": "Test Company",
"location": [
{
"id": 5185,
"value": "America"
},
{
"id": 5186,
"value": "USA"
}
]
},
"name": "John Doe"
}
]
}
I'm using the file template, but "result.data.id" doesn't return any value:
data_id,location_id,value
{{#each result.data}}
{{#each company.location}}
{{result.data.id}},{{id}},{{value}}
{{/each}}
{{/each}}
I want the following output:
data_id,location_id,value
35435,5185,America
35435,5186,USA
Am I missing something or should this output be produced using a different approach?
-
Hi Chad.
You can use "../" notation to look back one level. Based on the JSON you provided, the following template produces the output you're looking for. See the template in action here.
data_id,location_id,value
{{#each data}}
{{#each company.location}}
{{../id}},{{id}},{{value}}
{{/each}}
{{/each}}For more information on Handlebars in general, I recommend the Start Here post Getting Started with Handlebars in Chains.
1
Commenti
2 commenti