2016-11-23 48 views
0

我不知道是否有可能避免在自定義幫助器中使用..語法。如何避免在手柄幫手中使用..查找

比方說,我有這樣的幫手:

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

我發現自己是一個文檔解決方案: 使用@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}}