使用数组作为参数传递给 helper 中的函数,在侧 {{#each}} 中用于相同的数组(handlebars)
use array to pass as a parameter to a function in helper, in side {{#each}} for same array(handlebars)
我正在尝试在电子邮件模板中使用 {{#each}}。我想使用同一个数组作为参数传递给辅助函数,做一些事情。我无法在循环中获取数组的值。
这是我正在使用的代码。
Handlebars.registerHelper('ifSameCbeprev', function(index, msg, options) {
var ind = parseInt(index);
var x = ind - 1 ;
console.log(msg); //here value is undefined.
if(x<0)
{
return options.inverse(this);
}
else
{
console.log(msg);
if(msg[ind].cbe === msg[x].cbe)
{
return options.fn(this);
}
else
{
return options.inverse(this);
}
}
});
<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 中得到未定义。
知道如何在 {{#each}} 中使用数组吗?
在此先感谢您提供任何帮助。
我试过用这种方式代替签入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>
如果有任何其他解决方案也可以post。
我正在尝试在电子邮件模板中使用 {{#each}}。我想使用同一个数组作为参数传递给辅助函数,做一些事情。我无法在循环中获取数组的值。
这是我正在使用的代码。
Handlebars.registerHelper('ifSameCbeprev', function(index, msg, options) {
var ind = parseInt(index);
var x = ind - 1 ;
console.log(msg); //here value is undefined.
if(x<0)
{
return options.inverse(this);
}
else
{
console.log(msg);
if(msg[ind].cbe === msg[x].cbe)
{
return options.fn(this);
}
else
{
return options.inverse(this);
}
}
});
<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 中得到未定义。 知道如何在 {{#each}} 中使用数组吗? 在此先感谢您提供任何帮助。
我试过用这种方式代替签入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>
如果有任何其他解决方案也可以post。