Madhukar Hebbar
Madhukar Hebbar

Reputation: 3173

Underscore template : for statement dynamic values

I am using underscore template to fetch and display values from model. This below code working fine.

<% for (var i = 1; i <= data.total; i++) { %>
<td>List <%= i %>  </td>
<% } %> 

for the same i need to get the dynamic values which is stored in model like {List1,List2,List3 etc..} and need to display it in template.

for that i tried

 1. <% for (var i = 1; i <= data.total; i++) { %>
    <td><%- data.List<%= i %> %></td>
<% } %> 
 2. <% for (var i = 1; i <= data.total; i++) { %>
    <td><%- data.List${i}  %></td>
<% } %>

where data is model object and List1,List2..are its values.

Both the above codes are not working. I had just checked freemaker template which is supporting this kind of operation.

Freemaker Template for loop

Can we get same type or any other approach to achieve this?

Upvotes: 3

Views: 151

Answers (1)

mu is too short
mu is too short

Reputation: 434665

The interpolated parts of an Underscore template are just JavaScript expressions so you'd do it exactly the same way you'd do it normal JavaScript code (i.e. using [] and some string manipulation to build the keys):

<td><%- data['List' + i] %></td>

Upvotes: 3

Related Questions