How to use if else statements in underscore.js template ?

There is always a need to have a condition check when populating your underscore.js tempalte. For instance in my case I had a model and few fields in the model had null values so when they were rendered with these values, I got an output like the one shown below :

This clearly isn’t right, instead what should have happened here was, whenever the data in model was ‘null’ the text box should have been left empty. So here is how this is done in underscore.js template :

<div class="editor-label">
<label>First Name : </label>
<div class="editor-field">
<% if(FirstName == null) { %>
<input type="text" id="txtFirstName" value="" />
<% } else { %>
<input type="text" id="txtFirstName" value="<%=FirstName%>" />
<% } %>
Further Reading