ObviousCat
ObviousCat

Reputation: 505

Underscore template display array of objects via _.each

I'm having trouble printing out a simple for each comment loop with _.template.

<% _.each([comments], function(i) { %>  <p><%= i %></p> <% }); %>

prints [object Object]

<% _.each([comments], function(i) { %>  <p><%= JSON.stringify(i) %></p> <% }); %>

prints:

[{"comment":"Mauris quis leo at diam molestie sagittis.","id":263,"observation_id":25}]

What I've tried so far:

<% _.each([comments], function(i) { %>  <p><%= i.comment %></p> <% }); %>

blank

<% _.each([comments], function(i) { %>  <p><%= i.get('comment') %></p> <% }); %>

Uncaught TypeError: Object [object Array] has no method 'get'

<% _.each([comments], function(i) { %>  <p><%= comment %></p> <% }); %>

blank

Upvotes: 3

Views: 11499

Answers (1)

Scott Puleo
Scott Puleo

Reputation: 3684

Assuming comments is an array on your model:

<% _.each(comments, function(comment) { %>  
  <p><%= comment.comment %></p> 
<% }); %>

Upvotes: 15

Related Questions