Underline pattern: for dynamic operator values

I am using the underscore pattern to retrieve and display values โ€‹โ€‹from a model. This below code works fine.

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

      

for the same i need to get dynamic values โ€‹โ€‹that are stored in a model like {List1, List2, List3, etc.} and must display it in the template.

for what 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 objects and List1, List2..name of its values.

Both of the above codes don't work. I just checked out a freemaker template that supports this operation.

Freemaker template for loop

Is it possible to get the same type or any other approach to achieve this?

+3


source to share


1 answer


The interpolated parts of the subtree template are just JavaScript expressions, so you do it just like regular JavaScript code (i.e. using []

some string manipulation to create keys as well):



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

      

+3


source







All Articles