I can't get a simple switch statement working in my underscore template. It's using the value of a variable called UserType which I've checked exists by displaying it with <%= UserType %>.
Code coming up:
<% switch(UserType) { %>
<% case 13: %>
<button id="schoolButton" value="schools" class="gridChooser k-textbox">Schools</button>
<% case 12: %>
<button id="teacherButton" value="teachers" class="gridChooser k-textbox">Teacher</button>
<% case 8: %>
<button id="classButton" value="classes" class="gridChooser k-textbox">Classes</button>
<button id="testButton" value="tests" class="gridChooser k-textbox">Test</button>
<% } %>
Any help much appreciated - thanks.
You can do:
It isn't a "comfortable" solution, but neither is wrong. Just works.
The problem is that Underscore will add semicolon terminators when converting your template to JavaScript. So, a simple
switch
like this:becomes JavaScript that looks like this:
But a JavaScript
switch
needs to containcase
statements and an empty statement (i.e. the;
inswitch(x) { ;
) doesn't qualify.I can't think of any sane way around this problem so I'd just switch to an
if
and move on to more interesting problems:You could also turn it inside-out and use
print
:but that's a bit ugly (IMHO). See the
_.template
documentation for details.Note that this semicolon trickery is also why your
if
s must include braces in an Underscore template even if JavaScript doesn't require them. So this won't work:but this will:
The same applies to loops.