Ember CLI中的Handlebar帮助器语法 [英] Handlebar helper syntax in Ember CLI
问题描述
在这篇文章中
重复帮助器的示例被布局。
An example of a 'repeat' helper is layed out.
帮助
Handlebars.registerHelper('times', function(n, block) {
var accum = '';
for(var i = 0; i < n; ++i)
accum += block.fn(i);
return accum;
});
模板
template
{{#times 10}}
<span>{{this}}</span>
{{/times}}
我似乎不能写出' CLI的方式...有人可以启发我吗?
I can't seem to write this out the 'CLI' way... can someone enlighten me?
首先,它将是 / helpers $ c中的自己的帮助文件$ c>,它应该有一个破折号,因此解析器识别它。 - 所以我不会明确注册。
First of all, it will be it's own helper file in /helpers
, and it should have a dash so the resolver recognizes it. - so I wouldn't be registering it explicitly.
默认生成的助手看起来像这样 helpers / repeat-times .js
(模板应该是一样的...)
Default generated helper looks like this helpers/repeat-times.js
(template should be the same...)
import Ember from 'ember';
export function repeatTimes(input) {
return input;
}
export default Ember.Handlebars.makeBoundHelper(repeatTimes);
所以没有需要注册,不需要设置名称...我只是找不到清楚的文档的语法。 :/(我花了20个左右的刺...)
so, no need to register, no need to set the name... I just can't find clear docs on the syntax. :/ (I took 20 or so stabs at it...)
还是应该做一个组件?如下所示:使用ember-cli阻止帮助
Or should I be making a component instead? as suggested here: Block helper with ember-cli
推荐答案
@卡尔曼是对的,你不能用块符号注册绑定的帮助器,所以在这种情况下,我会推荐一个组件,这是一个参考在评论中。
@Kalman is right that you can't register a bound helper with block notation, so in this case I would recommend a component, which was referenced in the comment.
然而,对于那些可能已经发现这个问题,并且仍然想在ember-cli中创建一个句柄助手,你将需要使用 makeBoundHelper 函数。
However, for those that might have found this question and still want to create a handlebars helper in ember-cli, you'll want to use the makeBoundHelper function.
例如,这是一个当前日期帮助器,我使用:
For example, here's a current-date helper that I use:
// app/helpers/current-date.js
import Ember from 'ember';
export default Ember.Handlebars.makeBoundHelper(function() {
return moment().format('LL'); // Using moments format 'LL'
});
然后,在您的手柄模板中,您可以使用以下内容:
Then, in your handlebars template, you can use this:
{{current-date}}
2015年3月5日
这篇关于Ember CLI中的Handlebar帮助器语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!