【问题标题】:Get value of an array using dynamic index in handlebar template使用车把模板中的动态索引获取数组的值
【发布时间】:2014-03-14 07:38:09
【问题描述】:

我是 Handlebars 的新手。

我创建了一个在 CompositeView 中使用的 ItemView。此模板的值正确呈现。

var singleMonth = Marionette.ItemView.extend({
    tagName: 'tr',
    template: {
      type: 'handlebars',
      template: monthTemplate
    },
    months: [ 'JAN','FEB','MAR','APR','JUN','JUL','AUG','SEP','OCT','NOV','DEC' ],
    templateHelpers: function() {
      var helpers = {};
      helpers.months = this.months;
      return helpers;
    }
  });

这是我的模板

<td>{{ months.@index.[7] }}</td><td>{{ [12] }}</td>

我想根据 [7] 的值获取相应的月份值,该值将是月份数组的索引。

for Ex. if [7] is 3 then I want to get expression value as 'MAR'.

我不知道该怎么做。

你能告诉我该怎么做吗?

(注意:我不想在这里使用#each 或任何循环)

谢谢

【问题讨论】:

    标签: handlebars.js marionette


    【解决方案1】:

    尽管您的模板中可能存在语法错误,但模板本身不应该这么聪明。

    保持愚蠢就像

    <td>{{ thisMonth }}</td>
    

    然后在templateHelpers中构建thisMonth

    templateHelpers: function() {
      // The reason for _this is: `this` means model in templateHelpers.
      _this = this; 
      return {
        thisMonth: _this.months[7]
      }
    };
    

    【讨论】:

    • 我只这样做了...!!谢谢
    猜你喜欢
    • 2023-03-15
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多