Handlebars路径中的变量

时间:2015-10-04 21:29:45

标签: javascript templates handlebars.js

我正在尝试在我的Handlebars模板中引用一个变量,以便在我的#each块中使用。但是,我似乎无法让它发挥作用:

// doesn't work, logs nothing
{{#each collections.[this.sectionIndex] }}
    {{ log this.title }}
{{/each}}

该变量在页面中可用,由记录确认:

{{ log this.sectionIndex }} // --> writing

我目前看到的唯一区别是this.sectionIndex是上面的字符串,而下面的值是Handlebars语法的一部分,并且(我假设)处理方式不同:

// does work
{{#each collections.[writing] }}
    {{ log this.title }}
{{/each}}

供参考,这是collections的样子:

{ 
  pages: [ 
    { title: 'About Us',
      url: 'about',
      ...
    },
    ... 
  ],
  writing: [ 
    { title: 'page 2',
      contents: '...',
      ...
    },
    { title: 'page 1',
      contents: '<p>Writing goes here.</p>\n',
      ...
    }
  ]
}

我做错了什么?我是否需要使用帮助器?

0 个答案:

没有答案