Handlebars.js:如何在帮助器中调用partials?(Handlebars.js: how does partials gets invoked in a helper? I got: TypeError: fn is not a function)

我正在学习把手。 对于我来说,如何在帮助器中调用partials似乎仍然是个谜。

我读了这个教程: http : //blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers

从本教程中的这个例子中,

Handlebars.registerHelper("stripes", function(array, even, odd, fn) { var buffer = ""; for (var i = 0, j = array.length; i < j; i++) { var item = array[i]; // we'll just put the appropriate stripe class name onto the item for now item.stripeClass = (i % 2 == 0 ? even : odd); // show the inside of the block buffer += fn(item); <!-- this is where a partials gets invoked --> } // return the finished buffer return buffer; });

看来部分是由Handlebars添加和应用的。 但是,我在Handablebars 1.3.0和2.0 Alpha-2中使用了相同的方法,似乎不再是这种情况。 我总是得到错误:

TypeError: fn is not a function buffer += fn(item);

我做了很多在线搜索并找到了一些数字教程,但没有一个显示部分与1.3.0或更高版本中的助手相关联。

有人可以帮我吗?

非常感谢!

I am learning Handlebars. It still appears to be a mystery to me that how a partials gets invoked in a helper.

I read this tutorial: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers

From this example in the tutorial,

Handlebars.registerHelper("stripes", function(array, even, odd, fn) { var buffer = ""; for (var i = 0, j = array.length; i < j; i++) { var item = array[i]; // we'll just put the appropriate stripe class name onto the item for now item.stripeClass = (i % 2 == 0 ? even : odd); // show the inside of the block buffer += fn(item); <!-- this is where a partials gets invoked --> } // return the finished buffer return buffer; });

it appears the partial is added and applied by Handlebars. However, I used this same approach in Handablebars 1.3.0 and 2.0 Alpha-2, it seems no longer the case. I always got the error:

TypeError: fn is not a function buffer += fn(item);

I did quite online search and found a number tutorials, but none of them shows how partials is hooked up with a helper in version 1.3.0 or later.

Can someone help me out?

Thanks a lot!

最满意答案

好。 我相信我解决了这个问题。 在v1.0或更高版本中,Handlebars将所有内容放入哈希值,而不仅仅是fn。 所以,上述帖子中的所有内容仍然有效,除了以下两行:

Handlebars.registerHelper("stripes", function(array, even, odd, options) ... buffer += options.fn(item);

希望这有助于其他人。 欢迎任何确认。 我觉得这没有直接的例子,至少我没有找到它。

问候。

OK. I believed I solved this problem. In v1.0 or later, Handlebars puts everything in a hash, not just fn. So, everything in the above post still is valid except these two lines:

Handlebars.registerHelper("stripes", function(array, even, odd, options) ... buffer += options.fn(item);

Hope this helps someone else. Any confirmation is welcome. I feel bad that there is no direct example on this, at least I did not find it.

Regards.

更多推荐