how to tell if a property exists and is false

岁酱吖の 提交于 2019-12-03 07:29:16

You can use another else statement in there using a === false check, like this:

{{if Value}}
    Value exists and is true
{{else typeof(Value) != "undefined" && Value === false}}
    Value exists and is false
{{else}}
    Value doesn't exist or isn't explicitly false
{{/if}}

You can test it out here. The typeof check is because you'll get a Value is not defined error with only Value === false. You would add other checks as well, for example {{else typeof(Value) == "undefined"}} would be true if the value wasn't specified.

You could write a function to check for you:

$(document).ready(function() {
    function isExplicitlyFalse(f) { return f === false; }

    $("#testTemplate").tmpl({Test:1, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
    $("#testTemplate").tmpl({Test:2, Value:true, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
    $("#testTemplate").tmpl({Test:3, Value:false, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
});

then in your template:

{{if item.isExplicitlyFalse(Value)}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!