2015-01-05 20 views
3

我想如下的把手使用@index工作:@index不是在車把

<script id="some-template" type="text/x-handlebars-template"> 
<form class = "pod" action="#" method="post"> 

<table> 
<thead> 
    <th>Name</th> 
    <th>shipment</th> 
    <th>Button</th> 
</thead> 
<tbody> 
    {{#each objects}} 
    <tr> 
     <td>{{name}}</td> 
     <td> <input type="text" id="Shipment" name={{join 'Shipment' name}} /> </td> 
    <td> <input type="submit" name="actionButton" value = "update" > </td> 

    {{#if @index == 2 }}  
    <td> 
    <a href ="http://www.datatables.net/examples/api/select_single_row.html{{name}}"> {{name}} </a> 
    </td> 

    <td> </td> 
     {{/if}} 

{{/each}} 
</tbody> 
</table> 
</form> 
</script> 

但是我如果條件似乎並不奏效。任何人都可以告訴我我做錯了什麼?

回答

0

不幸的是,如果沒有自定義幫助器,您無法在Handlebars中進行比較。

我不記得在那裏我發現這個功能,但我有我正在使用中的一個節點/快遞腳本副本設置基於一個條件類:

app.js

var hbs = require('hbs'); 

hbs.registerHelper('ifCond', function (v1, operator, v2, options) { 
    switch (operator) { 
     case '==': 
      return (v1 == v2) ? options.fn(this) : options.inverse(this); 
     case '===': 
      return (v1 === v2) ? options.fn(this) : options.inverse(this); 
     case '<': 
      return (v1 < v2) ? options.fn(this) : options.inverse(this); 
     case '<=': 
      return (v1 <= v2) ? options.fn(this) : options.inverse(this); 
     case '>': 
      return (v1 > v2) ? options.fn(this) : options.inverse(this); 
     case '>=': 
      return (v1 >= v2) ? options.fn(this) : options.inverse(this); 
     case '&&': 
      return (v1 && v2) ? options.fn(this) : options.inverse(this); 
     case '||': 
      return (v1 || v2) ? options.fn(this) : options.inverse(this); 
     default: 
      return options.inverse(this); 
    } 
}); 

HBS模板

<div class="{{#ifCond this.order.pushed '==' 'true'}}text-green{{/ifCond}}">Order Status - {{this.order.pushedMessage}}</div>