0

I need convert Jade select list which iterates over statuses and assigns a status to an account into Handlebars template. In Jade it looks like this:

  select.form-control(name='status')
    option(value='') -- choose --
    for status in data.statuses
      option(value='#{status._id}') #{status.name}

I tried to do this with #each like this

<select name="status" class="form-control">
  <option value="">-- choose --</option>
  {{#each statuses}}
  <option value="{{this._id}}">{{this.name}}</option>
  {{/each}}
</select>

but it doesn't work because it is not an array.

Is there a way to do this with Handlebars? Thank you.

ASem
  • 122
  • 2
  • 14
  • Do you read this question: http://stackoverflow.com/questions/9058774/handlebars-mustache-is-there-a-built-in-way-to-loop-through-the-properties-of ? – inem88 May 03 '17 at 04:07
  • @Иван Емельянов yes I triied `{{#each statuses}} {{/each}}`. It didn't work. – ASem May 03 '17 at 08:15
  • I cant find many particular use cases for `@key` – ASem May 03 '17 at 08:23

1 Answers1

1

You can see this example:

http://jsfiddle.net/bby5ynuw/

var source = $("#some-template").html(); 
var template = Handlebars.compile(source); 

var data = { 
   statuses:[
    {_id:1, name:'name1'},
    {_id:2, name:'name2'}
  ],
  statuses2:{
    o1:{_id:1, name:'name1'},
    o2:{_id:2, name:'name2'},
    o3:{_id:3, name:'name3'}
  }
}; 

$('body').append(template(data));

Both variants (array and object) are working correct with your template. May be you set statuses into handlebars incorrect?

inem88
  • 308
  • 2
  • 13