stencilproject/Stencil

Group list of items in collection

rlaguilar opened this issue · 2 comments

Hi.
I have a datasource with a list of items and I want to display those items using several templates. Each of those templates will render the items in a table, but not all the tables would have the same amount of columns. For instance, if I have the items [Item1, Item2, Item3, Item4, Item5, Item6], I want to render them like:

Template1 (which uses 3 columns):

<table>
  <tr>
    <td>Item1</td>
    <td>Item2</td> 
    <td>Item3</td>
  </tr>
  <tr>
    <td>Item4</td>
    <td>Item5</td> 
    <td>Item6</td>
  </tr>
</table>

Template2 (which uses 2 columns):

<table>
  <tr>
    <td>Item1</td>
    <td>Item2</td> 
  </tr>
  <tr>
    <td>Item3</td>
    <td>Item4</td>
  </tr/
  <tr>
    <td>Item5</td> 
    <td>Item6</td>
  </tr>
</table>

Is there currently any way to accomplish this?

@rlaguilar you can either use one template and render it with different contexts, it should be 2d array of different sizes (2x3 and 3x2 respectively). Or you can use two different templates which will add </tr></td> after each 3nd or 2nd iteration of for loop respectively, and render them with plain array as a context.

You can not do operations on context data, if that's what you are looking for. You can look into StencilSwiftKit for some additional nodes, maybe you'll find something useful there.