ember-cli support for Handlebars @vars in each helper (i.e., @index, @key, @first, @last)

前端 未结 2 1583
北荒
北荒 2021-01-05 11:07

I am getting a compilation error in ember-cli whenever I have a Handelbars template that uses @vars variables (i.e., @index, @key, @first, @last) inside of the each helper.

相关标签:
2条回答
  • 2021-01-05 11:37

    That really isn't related to ember-cli. Ember Handlebars doesn't support the @keyword items.

    0 讨论(0)
  • 2021-01-05 11:50

    It's possible to mimic behavior of following Handlebars keywords: @index, @key, @first, @last.

    @index

    {{#each array as |item index|}}
      Index of item: `{{item}}` is: `{{index}}`
    {{/each}}
    

    @key

    {{#each-in object as |key value|}}
      {{key}}: {{value}}
    {{/each-in}}
    

    @first

    You could also mimic behavior of @first using ember-truth-helpers addon and taking advantage of eq helper - thanks to kristjan reinhold for this idea:

    {{#each array as |item index|}}
      {{#if (eq index 0)}}
        <!-- first element specific html -->
      {{else}}
        <!-- other html -->
      {{/if}}
    {{/each}}
    

    Instead of (eq index 0) you can use (eq item array.firstObject).

    @last

    As dwickern suggested you can use Ember.Array.lastObject to mimic @last behavior.

    {{#each array as |item|}}
      {{#if (eq item array.lastObject)}}
        <!-- last element specific html -->
      {{else}}
        <!-- other html -->
      {{/if}}
    {{/each}}
    
    0 讨论(0)
提交回复
热议问题