最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handlebars.js - loop an array excluding the first element? - Stack Overflow

programmeradmin3浏览0评论

For a bootstrap carousel item <div class="item"> the first item needs to be active
div class="item active"> though only the first the item

Thought to write a Handlebars Helper, to loop through like this:

  <div class="item active">
    <div class="foo">{{foo.[0]}}</div>
  </div>
{{#each resArray foo}}
  <div class="item">
    <div class="foo">{{this}}</div>
  </div>
{{/each}}

..though how would this be written correctly?

Handlebars.registerHelper("resArray", function(array) {
  return array[1 to array.length];
});

Also, where would this helper go? ..in my node server.js file where Handlebars is assigned?

For a bootstrap carousel item <div class="item"> the first item needs to be active
div class="item active"> though only the first the item

Thought to write a Handlebars Helper, to loop through like this:

  <div class="item active">
    <div class="foo">{{foo.[0]}}</div>
  </div>
{{#each resArray foo}}
  <div class="item">
    <div class="foo">{{this}}</div>
  </div>
{{/each}}

..though how would this be written correctly?

Handlebars.registerHelper("resArray", function(array) {
  return array[1 to array.length];
});

Also, where would this helper go? ..in my node server.js file where Handlebars is assigned?

Share Improve this question edited Apr 11, 2014 at 5:02 StackThis asked Apr 11, 2014 at 4:26 StackThisStackThis 8833 gold badges16 silver badges45 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

Turns out it's as easy as:

{{#each foo}}
  {{#if @first}}
    <div class="item active">
      <div class="foo">{{this}}</div>
    </div>
  {{else}}
    <div class="item">
      <div class="foo">{{this}}</div>
    </div>
  {{/if}}
{{/each}}

Use the following code:

{{#each foo}}
  <div class="item {{#if @first}}active{{/if}}">
    <div class="foo">{{this}}</div>
  </div>
{{/each}}
发布评论

评论列表(0)

  1. 暂无评论