Chapsterj
Chapsterj

Reputation: 6625

_underscore loop in a template

I keep getting an error with this. I keep getting a syntax error:

syntax error var _p=[],print=function(){_p.push.a... ');}return __p.join('');

 <script id="product" type="text/template">  
      <p><span>items</span><span class='items'><%= _.each(info.items, function(books) { %> 
      <%= books.name + ",&nbsp" %>
      <% }); %></span></p>
    </script>

Anyone know why this error is happening. I have looked at some other people using this style and it seems correct but maybe I'm missing a symbol?

Note: Fixed the issue. My code had a = sign in the wrong place.

<%= _.each(info.items, function(books) { %>

should be:

<% _.each(info.items, function(books) { %>

Not sure why you need the = for some areas and not for others. Maybe someone can explain. example in this area of the code I need to use a = sign :

<%= books.name + ",&nbsp" %>

Upvotes: 1

Views: 2127

Answers (2)

eschwartz
eschwartz

Reputation: 561

<%= variable %> is underscore shorthand for <% print variable %>

Upvotes: 1

evilcelery
evilcelery

Reputation: 16139

With the default settings, when you do <%= variable %> it just prints out the value of variable.

To evaluate (i.e. run a piece of Javascript code) you do <% alert('something') %>

Upvotes: 3

Related Questions