The following Pug script:
- data = [ \"A\", \"B\", \"C\", \"D\" ]
- for (i=0,i
Although sean's answer is correct it does contain code duplication because you have to specify what you want to do with the item, twice per row. His answer also doesn't scale well for situations where you need more than two items per row.
Instead of using the [i+1] structure by sean's I would suggest using a second loop to find all the items that have to fall into this row. This can be done using the following pattern:
- var itemsPerRow = 2; // This can be any positive number
each unused, i in data
if (i % itemsPerRow == 0)
.row
each item, j in data
if (j - i >= 0 && j - i < itemsPerRow)
.col
.. What you want to do with -item- ..
The line if (j - i >= 0 && j - i < itemsPerRow)
makes sure that only the items that actually fall into that row get render.
This avoids code duplication because you only have to type .. What you want to do with -item- ..
once.
The result looks like this
.row
.col
.col
.row
.col
.col
.row
.col
.col