I am using the _.template() function of underscorejs together with backbonejs. When using underscore.js v1.3.0, I could use an if statement as shown:
<script type="text/template" id="tpl_listing_list_item">
<% if(<%= address_2 %>){%>, <%= address_2 %><%}%>
</script>
Problem: After updating to v1.3.3, I get the error Uncaught SyntaxError: Unexpected token ILLEGAL in the Javascript console. Has this feature been removed? Removing the if code fixes the error. If it’s removed, is there another way to achieve the same thing?
In your
ifstatement you’ve already escaped into interpolation mode, so the<%=is an illegal character.This works when I use it in my browser with 1.3.3
Example:
(We use JSP so our template tags are
{% %},{{ }}, and{%- %}instead of the defaults, so excuse my tags)