2015-07-01 17 views
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}}中的數組? 在此先感謝您提供任何幫助。

+0

我對'handlebars.js'一無所知,但你確定你需要使用'#ifSameCbeprev'而不是'#if SameCbeprev'? –

+0

@VitorCanova是的。那是我創建的用戶定義的幫助函數。 – Shrabanee

+0

嗡嗡聲。尼斯。你確定它正在返回一些東西?你有沒有試過返回一個硬編碼值?或者你確定這是'@ index'的問題? –

回答

0

我試過用這種方法,而不是在html中檢查。這解決了我的問題。下面給出代碼。

var msgs = [{cbe : 'abc', ty : 'task'}, {cbe : 'abcd', ty : 'task'}]; 
 

 
for(var k in msgs) 
 
    { 
 
    if(k != 0) 
 
     { 
 
      var sameSender = getCFna(msgs[k-1].cbe , msgs[k].cbe); 
 
    
 
    if(sameSender == "same") 
 
    { 
 
     msgs[k].creatorName = ""; 
 
    } 
 
    else 
 
    { 
 
     msgs[k].creatorName = msgs[k].cfna ? msgs[k].cfna : msgs[k].cbe; 
 
    } 
 
    
 
    console.log(msgs[k].creatorName); 
 
     } 
 
    } 
 

 
function getCFna(prevem , currentEm) 
 
{ 
 
    if(prevem.toLowerCase().trim() == currentEm.toLowerCase().trim()) 
 
    { 
 
     return "same"; 
 
    } 
 
    else 
 
    { 
 
     return "not same"; 
 
    } 
 
};
<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;"> 
 
         <span>{{creatorName}}</span> 
 
       </td> 
 
      </tr> 
 
     </table> 
 
    </td> 
 
    </tr> 
 
    {{/each}} 
 
    </body> 
 
    </html>

如果沒有其他解決辦法是存在的,也張貼。

相關問題