我想知道是否可以避免在自定义助手中使用..语法。
让我们说我有这个帮手:
Handlebars.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
是否可以使用它而无需在其中添加..以供参考:
{{#for 0 5 1}}
{{#ifCond1OrCond2 (lookup ../bid this) (lookup ../ask this)}}
<tr>
<td>{{math this "+" 1}}</td>
<td>{{#with (lookup ../../bid this)}}{{quantity}}{{/with}}</td>
<td>{{#with (lookup ../../bid this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup ../../ask this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup ../../ask this)}}{{quantity}}{{/with}}</td>
</tr>
{{/ifCond1OrCond2}}
{{/for}}
这是一个小提琴https://jsfiddle.net/ChristopheThiry/f9wu07f4/,显示正在发生的事情
以下是我想要使用的语法:
{{#for 0 5 1}}
{{#ifCond1OrCond2 (lookup bid this) (lookup ask this)}}
<tr>
<td>{{math this "+" 1}}</td>
<td>{{#with (lookup bid this)}}{{quantity}}{{/with}}</td>
<td>{{#with (lookup bid this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup ask this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup ask this)}}{{quantity}}{{/with}}</td>
</tr>
{{/ifCond1OrCond2}}
{{/for}}
答案 0 :(得分:0)
我发现自己是文档中的解决方案: 使用@root完成工作,因为它从根元素开始。
{{#for 0 5 1}}
{{#ifCond1OrCond2 (lookup @root/bid this) (lookup ask this)}}
<tr>
<td>{{math this "+" 1}}</td>
<td>{{#with (lookup @root/bid this)}}{{quantity}}{{/with}}</td>
<td>{{#with (lookup @root/bid this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup @root/ask this)}}{{price}}{{/with}}</td>
<td>{{#with (lookup @root/ask this)}}{{quantity}}{{/with}}</td>
</tr>
{{/ifCond1OrCond2}}
{{/for}}