2
我試圖在電子郵件模板中使用{{#each}}。我想使用相同的數組作爲參數傳遞給幫助函數,做一些事情。我無法獲得循環內數組的值。使用數組作爲參數傳遞給helper中的函數,同一數組(handlebars)中的{{#each}}方向
這是我使用的代碼。
Handlebars.registerHelper('ifSameCbeprev', function(index, msg, options) { \t
\t var ind = parseInt(index);
\t var x = ind - 1 ;
console.log(msg); //here value is undefined.
\t if(x<0)
\t { \t \t
\t \t return options.inverse(this);
\t }
\t else
\t {
\t \t console.log(msg);
\t \t if(msg[ind].cbe === msg[x].cbe)
\t \t { \t \t \t
\t \t \t return options.fn(this);
\t \t }
\t \t else
\t \t { \t \t \t
\t \t \t return options.inverse(this);
\t \t }
\t }
\t \t
});
<html>
<body>
{{#each msg}}
<!-- If task is created -->
{{#iftytask ty}}
<tr width="100%" height="auto" style=" min-height:40px;">
<td width="100%" height="100%">
<table width="100%" height="auto" style="padding:10px;">
<tr width="100%" height="auto"style="position:relative;">
<td width="98%" height="100%" style="font-size:16px; font-weight: bold;">
{{#ifSameCbeprev @index ../msg}}
{{else}}
<span>{{cfna}}</span>
{{/ifSameCbeprev}}
</td>
</tr>
</table>
</td>
</tr>
{{/each}}
</body>
</html>
我使用的代碼是如上。 ../msg給出未定義(即ifSameCbeprev中的msg)。我嘗試使用'this',msg和../../msg以及../msg。但是總是我在函數ifSameCbeprev中獲取undefined。 任何想法如何使用{{#each}}中的數組? 在此先感謝您提供任何幫助。
我對'handlebars.js'一無所知,但你確定你需要使用'#ifSameCbeprev'而不是'#if SameCbeprev'? –
@VitorCanova是的。那是我創建的用戶定義的幫助函數。 – Shrabanee
嗡嗡聲。尼斯。你確定它正在返回一些東西?你有沒有試過返回一個硬編碼值?或者你確定這是'@ index'的問題? –