开发者

How do I make alternating row colors in a Mustache.js template?

I have a template:

{{#people}}
  <div style="background-color: **gray/white**;"><span>{{name}}</span>: <span>{{title}}</span></div>
{{/people}}

Is there a way to set that background-color without passing it in from my controller? This is purely display and I do not feel that it belongs in my controller as a result, so would like to avoid naming colors there if p开发者_StackOverflowossible.


As I mentioned in the comment, Mustache can not solve this, however I found a mustache.js-specific way:

var template = '{{#people}}'
  +'  <div style="background-color: {{color}}"><span>{{name}}</span>: <span>{{title}}</span></div>'
  +'{{/people}}'

var data = {
  people: [
    {name: 'a', title: 'b'},
    {name: 'c', title: 'd'},
    {name: 'e', title: 'f'},
    {name: 'g', title: 'h'},
    {name: 'i', title: 'j'},
  ],
  color: function() {
    return window.divcolor = window.divcolor == 'gray' ? 'white' : 'gray'
  }
}

Mustache.to_html(template, data)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜