【问题标题】:@index not working in handlebars@index 在车把中不起作用
【发布时间】:2015-01-05 21:07:10
【问题描述】:

我正在尝试在车把中使用@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>

但我的 if 条件似乎不起作用。谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript templates handlebars.js


    【解决方案1】:

    不幸的是,如果没有自定义助手,您将无法在 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>
    

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 2021-06-19
      • 2020-12-24
      • 2018-08-03
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      相关资源
      最近更新 更多