Search code examples
jqueryjquery-pluginsjquery-templates

Checking for property on the object passed to jquery tmpl


I have a sample data like this:

data = {opt1: 'One', opt2:'Two', opt3:'Three'};

tmplString = <li>${opt1}</li><li>${opt2}</li>??IF OPT3?? 

$('#node').append(tmplString, data);

In my tmplString I want to render opt3 only if the passed in data has a property called opt3. If I use {{if opt3}} it throws an error when there is no opt3 in the data. Is there a way I could use something like if(prop in obj).


Solution

  • You should be able to use:

    if(typeof opt3 == "string")
    

    Or, if you might have something other than a string in there:

    if(typeof opt3 != "undefined")