I have model:
[
{
"ID": 5,
"email": "[email protected]"
},
{
"ID": 6495,
"email": "[email protected]"
}
]
Code for iterating in handlebars:
{{#each xxx}}
<p>{{email}}</p>
{{/each}}
how do I define xxx ?
If JSON had name in model like:
users: [
{
"ID": 5,
"email": "[email protected]"
},
{
"ID": 6495,
"email": "[email protected]"
}
]
I would simple iterate in handlebars like:
{{#each users}}
<p>{{email}}</p>
{{/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.
The foreach helper will output the content placed between its opening and closing tags {{#foreach}}{{/foreach}} once for each item in the collection passed to it. The {{#foreach}} helper is context-aware and should always be used instead of Handlebars each when working with Ghost themes.
Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression. For example, in checking for initialization of a variable the built-in #if check might not be appropriate as it returns false for empty collections (see Utils. isEmpty).
This works too:
{{#each this}}
<p>{{email}}</p>
{{/each}}
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