如果条件不起作用,手把 [英] Handlebar if condition not working

查看:107
本文介绍了如果条件不起作用,手把的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果条件在Handlbars模板中无法使用
下面是我下面的jquery Handlebars函数,

  $。each(data.response,function(i,item) {
var source = $(#my-orders)。html();
var template = Handlebars.compile(source);
html + = template(item);
// alert(html);
}); (html){
$(#myorderss)。html(html);

}

当我使用 if
$ $ $ $ $ $ $ {$#$> $ {$# {/ if}}

如果如果条件在句柄模板?

解决方案

这是if条件的误解。
请看这里的#if文档: http://handlebarsjs.com/builtin_helpers.html


您可以使用if助手来有条件地渲染块。如果它的
参数返回false,undefined,null,,0或[],Handlebars将
不呈现该块。

你需要做的是一个手柄助手来测试你的情况。



下面是一个我使用的测试助手的例子比较:

$ p $ Handlebars.registerHelper('test',function(lvalue,operator,rvalue,options){
var doDisplay = false;
var items =(+ rvalue).split(|);
var arrayLength = items.length;
for(var i = 0;(i < arrayLength); i ++){
if(operator ==eq){
if(lvalue == items [i]){
doDisplay = true;
}
} else if(operator ==ne){
if(lvalue!= items [i]){
doDisplay = true;
}
} else if(parseFloat(lvalue)> parseFloat(items [i])){
doDisplay = true;
}
} else if(operator ==lt){
if(parseFloat(lvalue)< parseFloat(items [i])){
doDisplay = true;
}
} else if(operator ==le){
if(parseFloat(lvalue)< = parseFloat(items [i])){
doDisplay = true ;
}
} else if(operator ==ge){
if(parseFloat(lvalue)> = parseFloat(items [i])){
doDisplay = true ;
}
}
}
if(doDisplay){
return options.fn(this);
} else {
return;
}
});

以下是您如何调用它以及如何测试并显示取决于test:

  {{#test id'eq''1'}} 
{{id}}等于1
{{/ test}}
{{#test id'ne''1'}}
{{id}}不等于1
{{/ test}}
{{#test id'ge''1'}}
{{id}}大于或等于1
{{/ test}}
{{#test id' ne''1'}}
{{id}}小于等于1
{{/ test}}


Unable to use if condition in Handlbars template. Below is my jquery Handlebars function below,

$.each(data.response, function (i, item) {
   var source = $("#my-orders").html();
   var template = Handlebars.compile(source);
   html += template(item);
   //alert(html);
});
if(html){
   $("#myorderss").html(html);
}

When I'm using if condition, its not working.

{{#if id=='1'}}
{{/if}}

How to use if condition in handlebars template?

解决方案

This is a misunderstanding of the if condition. Look at the #if documentation here : http://handlebarsjs.com/builtin_helpers.html

You can use the if helper to conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], Handlebars will not render the block.

What you need to do is a handlebar helper to test your condition.

Here is one example of a "test" helper that I use to do comparisons :

Handlebars.registerHelper('test', function(lvalue, operator, rvalue, options) {
    var doDisplay = false;
    var items = (""+rvalue).split("|");
    var arrayLength = items.length;
    for (var i = 0; (i < arrayLength); i++) {
        if (operator == "eq") {
            if (lvalue == items[i]) {
                doDisplay = true;
            }
        } else if (operator == "ne") {
            if (lvalue != items[i]) {
                doDisplay = true;
            }
        } else if (operator == "gt") {
            if (parseFloat(lvalue) > parseFloat(items[i])) {
                doDisplay = true;
            }
        } else if (operator == "lt") {
            if (parseFloat(lvalue) < parseFloat(items[i])) {
                doDisplay = true;
            }
        }else if (operator == "le") {
            if (parseFloat(lvalue) <= parseFloat(items[i])) {
                doDisplay = true;
            }
        }else if (operator == "ge") {
            if (parseFloat(lvalue) >= parseFloat(items[i])) {
                doDisplay = true;
            }
        }
    }
    if (doDisplay) {
        return options.fn(this);
    } else {
        return "";
    }
}); 

and here is how you call it for example if you and to test and display a result depending on the test:

{{#test id 'eq' '1'}}
{{id}} equals 1
{{/test}}
{{#test id 'ne' '1'}}
{{id}} do not equals 1
{{/test}}
{{#test id 'ge' '1'}}
{{id}} greater or equals 1
{{/test}}
{{#test id 'ne' '1'}}
{{id}} lower or equals 1
{{/test}}

这篇关于如果条件不起作用,手把的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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