@index不能在句柄中工作 [英] @index not working in handlebars

查看:138
本文介绍了@index不能在句柄中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在句柄中使用@index,如下所示:

 < script id =some-templatetype = 文本/ X-车把模板 > 
< form class =podaction =#method =post>

< table>
< thead>
< th>名称< / th>
货件< / th>
< th>按钮< / th>
< / thead>
< tbody>
{{#each objects}}
< tr>
< td> {{name}}< / td>
< td> < input type =textid =Shipmentname = {{join'Shipment'name}} /> < / TD>
< td> < input type =submitname =actionButtonvalue =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>

但是,如果条件似乎不起作用。任何人都可以告诉我我做错了什么?

解决方案

不幸的是, / p>

我不记得我在哪里找到这个函数,但是我有一个副本,我在一个node / express脚本中根据条件设置类:

app.js 要求( 'HBS');
$ b hbs.registerHelper('ifCond',function(v1,operator,v2,options){
switch(operator){
case'==':
返回(v1 == v2)?options.fn(this):options.inverse(this);
case'===':
return(v1 === v2)?options.fn这个):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( (> 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(t他);
默认值:
return options.inverse(this);
}
});

hbs模板

 < div class ={{#ifCond this.order.pushed'==''true'}} text-green {{/ ifCond}}>订单状态 -  {{this.order.pushedMessage}}< / div> 


I am trying to use @index in handlebars as follows:

<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>

But my if condition does not seem to be working. Can anyone tell me what i am doing wrong?

解决方案

You can't do comparisons in Handlebars without a custom helper unfortunately.

I can't recall where I found this function, but I had a copy I was using in a node / express script to set the class based on a condition:

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 template

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

这篇关于@index不能在句柄中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆