3

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?

1 Answer 1

3

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>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.