最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make an && operator in a JSrender if statement? - Stack Overflow

programmeradmin0浏览0评论

I need the following if statement in a JSRender template:

if (UltimoStr != "0,00" && BudgetStr != "0,00" && ReseveretStr != "0,00")
{
<div class="Posts"></div>  
}

but i can't figure out the syntax.

What i have so far is:

{{if UltimoStr != "0,00"}} and {{BudgetStr != "0,00"}} and {{ReseveretStr != "0,00"}}
<div class="Posts"></div>  
{{/if}} 

which isn't working.

I've been looking at this site for documentation, but it doesnt seem to have multiple and operators:

.html

I need the following if statement in a JSRender template:

if (UltimoStr != "0,00" && BudgetStr != "0,00" && ReseveretStr != "0,00")
{
<div class="Posts"></div>  
}

but i can't figure out the syntax.

What i have so far is:

{{if UltimoStr != "0,00"}} and {{BudgetStr != "0,00"}} and {{ReseveretStr != "0,00"}}
<div class="Posts"></div>  
{{/if}} 

which isn't working.

I've been looking at this site for documentation, but it doesnt seem to have multiple and operators:

https://github./BorisMoore/jsrender/blob/master/demos/scenarios/02_separators-scenario.html

Share Improve this question asked May 23, 2013 at 14:17 KenciKenci 4,88216 gold badges69 silver badges113 bronze badges 1
  • 1 It looks as if you misinterpreted the sample above (separators scenario) in which the {{if}} {{else}} is being used to insert the string separator "and". So the "and" is nothing to do with the logical operator &&. This is a simpler example which does use "&&" :github./BorisMoore/jsrender/blob/master/demos/step-by-step/…. The reply from JcFx below looks correct to me... – BorisMoore Commented May 24, 2013 at 22:03
Add a ment  | 

1 Answer 1

Reset to default 4

This should work:

{{if UltimoStr != "0,00" && BudgetStr != "0,00" && ReseveretStr != "0,00"}}
    <div class="Posts"></div>  
{{/if}}

Alternatively, you could decorate the object you are binding with a single Boolean that calculates the result of that expression.

A list of operators that work in JsRender can be found here: http://msdn.microsoft./en-us/magazine/hh975379.aspx (scroll down to the section titled Expressions).

Here's a simple test:

HTML:

<body>
    <h2>Result</h2>
    <div id="expect-true"></div>
    <div id="expect-false"></div>
</body>

<script id="template" type="text/x-jsrender">
    {{:UltimoStr}}
    {{:BudgetStr }}
    {{:ReseveretStr }}
    {{if UltimoStr != "0,00" && BudgetStr != "0,00" && ReseveretStr != "0,00"}}
        <h1>True!</h1>
    {{else}}
        <h1>False!</h1>
    {{/if}}
</script>

//JS:
$(document).ready(function() {
    var trueData = {
        UltimoStr: "1,00",
        BudgetStr: "1,00",
        ReseveretStr: "1,00"
    };
    $("#expect-true").append($("#template").render(trueData));

    var falseData = {
        UltimoStr: "0,00",
        BudgetStr: "0,00",
        ReseveretStr: "0,00"
    };
    $("#expect-false").append($("#template").render(falseData));
});

Seems to work as expected: http://jsfiddle/FJSkh/2/

发布评论

评论列表(0)

  1. 暂无评论