stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 223 forks source link

Group list of items in collection #156

Closed rlaguilar closed 6 years ago

rlaguilar commented 6 years ago

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?

ilyapuchka commented 6 years ago

@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.

ilyapuchka commented 6 years ago

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.