Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over array in handlebar template without defined name in model

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}}
like image 375
jmav Avatar asked Jul 27 '12 03:07

jmav


People also ask

How do you iterate objects in Handlebars?

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.

What is the syntax for each loop in Handlebars?

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.

What is helpers in Handlebars?

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).


1 Answers

This works too:

{{#each this}}
<p>{{email}}</p>
{{/each}}
like image 200
Dave Stibrany Avatar answered Oct 01 '22 02:10

Dave Stibrany