Javascript Handlebars JS:是否可以将变量从一个助手传递到另一个助手?
我正在尝试构建一个HTML,它将用于我的最终用户希望用于产品旋转木马的jQuery循环旋转木马插件中。该插件要求HTML如下所示:Javascript Handlebars JS:是否可以将变量从一个助手传递到另一个助手?,javascript,templates,handlebars.js,Javascript,Templates,Handlebars.js,我正在尝试构建一个HTML,它将用于我的最终用户希望用于产品旋转木马的jQuery循环旋转木马插件中。该插件要求HTML如下所示: <div id="slide-0"><!-- anything in here --></div> <div id="slide-1"><!-- anything in here --></div> <div id="slide-2"><!-- anything in here
<div id="slide-0"><!-- anything in here --></div>
<div id="slide-1"><!-- anything in here --></div>
<div id="slide-2"><!-- anything in here --></div>
<!-- etc. -->
这是我想建立的车把模板。我不能使用默认的{{{#each}}
助手,因为我需要创建外部的“slide”div{{#createCarouselSlides}
是一个自定义帮助程序,最终用户在其中输入他/她想要创建的幻灯片数量
模板
{{#createCarouselSlides 2 products}}
<div id="slide-{{@index}}">
{{#customFunctionInner 2 ??? products}}
<div class="product">
<span class="product-name">{{Name}}</span>
</div>
{{/customFunctionInner}}
</div>
{{/createCarouselSlides}}
{{#createCarouselSlides 2 products}}
<div id="slide-{{@index}}">
{{#createCarouselItemr 2 ../products}}
<div class="product">
<span class="product-name">{{Name}}</span>
</div>
{{/createCarouselItem}}
</div>
{{/createCarouselSlides}}
我的问题是:我可以从我的第一个助手那里获取{{@index}}
并将其传递给另一个自定义助手吗?语法是什么样的?
在普通Javascript中,作为一对外部(i)和内部(j)for循环,这通常是“容易”的,外部循环控制幻灯片div的生成,并将i传递给内部循环。我解决了我的问题。它需要在外部帮助器中为索引创建一个私有变量,并通过options.data.index在内部帮助器中访问它。由于内部辅助对象与子模板关联,因此内部辅助对象可以访问变量 模板
{{#createCarouselSlides 2 products}}
<div id="slide-{{@index}}">
{{#customFunctionInner 2 ??? products}}
<div class="product">
<span class="product-name">{{Name}}</span>
</div>
{{/customFunctionInner}}
</div>
{{/createCarouselSlides}}
{{#createCarouselSlides 2 products}}
<div id="slide-{{@index}}">
{{#createCarouselItemr 2 ../products}}
<div class="product">
<span class="product-name">{{Name}}</span>
</div>
{{/createCarouselItem}}
</div>
{{/createCarouselSlides}}
{{#createCarouselSlides 2产品}
{{{#createCarouselItemr 2../products}
{{Name}}
{{/createCarouselItem}}
{{/createCarouselSlides}
助手
; (function ($, Handlebars, window, document, undefined) {
/* Outer function */
Handlebars.registerHelper('createCarouselSlides', function (productsPerSlide, context, options) {
var result = "";
/* Create the necessary number of slides */
for (var i = 0; i < Math.ceil(context.length / productsPerSlide); i += 1) {
if (options.data) {
data = Handlebars.createFrame(options.data || {});
data.index = i;
}
result += options.fn(context[i], { data: data });
}
return result;
});
/* Inner Function */
Handlebars.registerHelper('createCarouselItem', function (productsPerSlide, context, options) {
var result = "",
currentItemIndex = "";
/* Create the necessary number of items per slide */
for (var j = 0; j < productsPerSlide; j += 1) {
currentItemIndex = (options.data.index * productsPerSlide) + j;
if (currentItemIndex < context.length) {
result += options.fn(context[currentItemIndex]);
}
}
return result;
});
})(jQuery, Handlebars, window, document);
;(功能($,把手,窗口,文档,未定义){
/*外部功能*/
Handlebar.registerHelper('createCarouselSlides',函数(productsPerSlide,上下文,选项){
var结果=”;
/*创建必要数量的幻灯片*/
对于(var i=0;i