218

As the title of question says, is there a mustache/handlebars way of looping through an object properties?

So with

var o = {
  bob : 'For sure',
  roger: 'Unknown',
  donkey: 'What an ass'
}

Can I then do something in the template engine that would be equivalent to

for(var prop in o)
{
    // with say, prop a variable in the template and value the property value
}

?

Ben
  • 19,033
  • 11
  • 65
  • 105

8 Answers8

461

Built-in support since Handlebars 1.0rc1

Support for this functionality has been added to Handlebars.js, so there is no more need for external helpers.

How to use it

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.

Jon
  • 396,160
  • 71
  • 697
  • 768
  • This doesn't work with nested objects. I tried {{#each myObject}}
  • {{data.title}}
  • and it returns
  • – Rafi Jul 22 '13 at 16:22
  • 2
    @Rafi: one cannot make much sense of that without knowing your data structure though. – Jon Jul 22 '13 at 18:11
  • I had a list of strings i used as input for my template, it worked greate with the following template code {{#.}}
  • {{this}}
  • {{/.}} – sjkp Sep 09 '13 at 18:59
  • @Ben: can you make this the accepted answer? The older answers imply that this is not actually built-in, which was true at the time, but are now very misleading. – mcw Sep 25 '13 at 19:49
  • 4
    @Rafi: don't you mean {{this.title}}? – nevyn Oct 17 '13 at 09:43
  • Ok this slightly doesnt make sense. As I understand {{#each }} has to be an explicit key, where did you get myArray and myObject from? Your code would not work on a top-level object that doesnt have pre-defined keys. How do you this generically, over an object? – qodeninja Feb 10 '14 at 20:42
  • 2
    @qodeninja: Simple: the same way you refer to the values in the examples above -- with `{{#each this}}`. Your choice of terms is also confusing (what makes one object "top level" and another not? what are "pre-defined" keys exactly? etc), so you might want to revisit these concepts. – Jon Feb 11 '14 at 09:42
  • @Jon thanks for explaining this, I'm just getting into handlebars. Top-level as in not a subobject. data = { key1: val1, key2 : val2, sub1 : { } }, pre-defined keys, as in you have to explicitly specify the keys in the template. If you dont know what the keys are how do you do this? But you explained by using `this` so I'll try that! But the mystery is where you go myArray and myObject from, you should update your code to reflect an example that will work to help newbies who dont understand what you're implying – qodeninja Feb 11 '14 at 18:25
  • Not working for me. `Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed {0: Name can't be blank, 1: Zip code can't be blank, 2: Company type can't be blank, 3: Address line can't be blank, 4: Country can't be blank, 5: State can't be blank}` – sergserg Aug 25 '14 at 17:59
  • @Serg: Handlebars? What version? – Jon Aug 25 '14 at 18:52
  • Latest of this comment. 1.3.0. – sergserg Aug 25 '14 at 19:00
  • 1
    if not mistaken then only with v1.1.0 this is available, but great answer thanks. – Renars Sirotins Jul 01 '15 at 14:23
  • 2
    How do you do this for only a specific whitelist of properties? – Marco Prins Jul 28 '15 at 14:43
  • 1
    @MarcoPrins: In Handlebars at least, AFAIK you write your own helper. Handlebars is notorious for choosing to provide such a minimal amount of logic built-in that you cannot even stitch a whitelist together. – Jon Jul 28 '15 at 22:04
  • 1
    Using node and `mustache --version` is 2.3... **ERROR**: Error: Unclosed section "each paths". Where *paths* is the *myObject* name. – Peter Krauss Mar 06 '17 at 22:15
  • What if you need to do some manipulation or calculation to `@key`? For example the equivalent of `@key - 1`. How is that possible? In a lot of templates you need to do things such as checking the previous value within an array to avoid displaying duplicate headings, etc. – Andy Jul 02 '19 at 14:30