简体   繁体   English

Handlebars.js-循环除第一个元素外的数组?

[英]Handlebars.js - loop an array excluding the first element?

For a bootstrap carousel item <div class="item"> the first item needs to be active 对于引导轮播项目<div class="item"> ,第一项需要处于活动状态
div class="item active"> though only the first the item div class="item active">尽管只有第一个项目

Thought to write a Handlebars Helper, to loop through like this: 想写一个Handlebars Helper,像这样循环遍历:

  <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? ..在分配了把手的节点server.js文件中?

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}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM