user1937021
user1937021

Reputation: 10781

if statement within underscore loop

I'm trying to add this if statement in a loop in an underscore template:

<% _.each( looks, function( listItem, index ){ %>   
             <% if(_.contains(firstBatch, listItem.id)){ %> 
                <% if (index % 2 == 0) { %>
                        <div class="large-6 column overlay-col">  
                <% }  else { %>
                        <div class=" column overlay-col">
                <% } % %>
            <% } %>                                                                  
<% }) %>

Basically it's to detect if index is an even number, but I get this console error:

SyntaxError: missing : after property id
if (index % 2 === 0) {

what's wrong with code?

Upvotes: 0

Views: 70

Answers (1)

dthree
dthree

Reputation: 20730

I think your syntax has a bozo on line 7:

 <% } % %>

What's that extra percent?

This becomes apparent when you remove all the decoration around the function:

_.each( looks, function( listItem, index ){    
  if(_.contains(firstBatch, listItem.id)){  
    if (index % 2 == 0) { 
      <div class="large-6 column overlay-col">  
    }  else { 
      <div class=" column overlay-col">
    } % 
  }                                                                   
}) 

Upvotes: 1

Related Questions