Skip to content Skip to sidebar Skip to footer

How To Tell If A Property Exists And Is False

I am having a hard time determining if data passed into the jquery template exists and is false without getting errors. This is what I am using to test &l

Solution 1:

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.


Solution 2:

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

Post a Comment for "How To Tell If A Property Exists And Is False"