If the typo is corrected, Tom's answer is essentially correct, but I would take a different approach.
The reason you want to break the list up into groups is that you want it to look a certain way. So I think this should definitely stay in the view.
The necessary JavaScript code can run just as easily inside the Pug file as an unbuffered code block.
Finally, JavaScript has improved a bit and we can take advantage of some features that may not have been available when this question was written. Here's an example.
-
let i = 0
rows = products.reduce((accumulator, currentValue, currentIndex) => {
if (currentIndex > 0 && currentIndex % 3 === 0) i++
if (!Array.isArray(accumulator[i])) accumulator[i] = []
accumulator[i].push(currentValue)
return accumulator
}, [])
each row in rows
.row
each product in row
.product.simpleCart_shelfItem.col-md-4
p= product.name
I know this is an old question, but I had the same question today. The best submitted answer led me down the right path, but it had a typo, it was outdated, and it broke my idea of separating the data from the view. This is what worked for me.