How to determine if a property exists and whether it is false

It is difficult for me to determine if the data passed to the jquery template exists and false, without errors. This is what I use for testing.

<html> <head> <title>jQuery Templates {{if}} logic</title> </head> <body> <p id="results"></p> <p>How do you test if the Value exists and is false?</p> <script id="testTemplate" type="text/html"> Test ${Test}: {{if Value}} Value exists and is true {{else}} Value doesn't exist or is false {{/if}} <br/> </script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="jquery.tmpl.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#testTemplate").tmpl({Test:1}).appendTo("#results"); $("#testTemplate").tmpl({Test:2, Value:true}).appendTo("#results"); $("#testTemplate").tmpl({Test:3, Value:false}).appendTo("#results"); }); </script> </body></html> 

Does anyone know how to do this?

+6
javascript jquery templates jquery-templates
source share
2 answers

You can use another else where the check === false , for example:

 {{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 check it out here . The typeof check is that you will get a Value is not defined error only with Value === false . You would also add other checks, for example {{else typeof(Value) == "undefined"}} would be true if no value was specified.

+6
source share

You can write a function to check:

 $(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)}} 
+1
source share

All Articles