[英]How to avoid using a .. lookup inside a handlebar helper
I wonder if it is possible to avoid using the .. syntax inside a custom helper. 我想知道是否有可能避免在自定义帮助程序中使用..语法。
Let's say that I have this helper : 假设我有这个助手:
Handlebars.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
is it possible to use it without having to put .. for references inside it : 是否有可能使用它而不必在其中放入..作为参考:
{{#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}}
Here is a fiddle https://jsfiddle.net/ChristopheThiry/f9wu07f4/ that show what is going on 这是一个小提琴https://jsfiddle.net/ChristopheThiry/f9wu07f4/ ,显示了正在发生的事情
Here is the syntax I'd like to use : 这是我想使用的语法:
{{#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}}
I found myself a solution in documentation : using @root does the job as it starts from the root element. 我在文档中找到了解决方案:使用@root可以完成工作,因为它是从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}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.