I'm passing this into my template.
modules: {
left: [],
center: ['card', 'progressChart', 'tableOfChildren'],
right: ['activity', 'details', 'triggers']
}
I want to do something like this... (psuedo code that I don’t think will work)
{{#each region in modules}}
<div class="{{region}}">
{{#each region}}
<div class="{{this}} module"></div>
{{/each}}
</div>
{{/each}}
To iterate over an object with JavaScript and Handlebars. js, we can use the #each keyword. to add the Handlebars script and the template for looping through each entry in data . We use #each this to loop through each array property.
A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}
Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.
You can do
{{#each modules}}
<div class="{{@key}}">
{{#each this}}
<div class="{{this}} module"></div>
{{/each}}
</div>
{{/each}}
Reference: http://handlebarsjs.com/builtin_helpers.html#iteration
Probably duplicate, where Jon
provides the best solution:
For arrays:
{{#each myArray}}
Index: {{@index}} Value = {{this}}
{{/each}}
For objects:
{{#each myObject}}
Key: {{@key}} Value = {{this}}
{{/each}}
Note that only properties passing the hasOwnProperty
test will be enumerated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With