How to render fixed length rows of items

Hey folks,

if you find yourself in a situation where you need to write a template that splits up a list of items in multiple rows with each row having a fixed amount of items, here is a simple solution:

  1. <div class="items">
  2.   <?php while ($items): ?>
  3.     <div class="row">
  4.       <?php for ($i = 0; ($item = array_shift($items)) && $i < 4; $i++): ?>
  5.         <div class="item">
  6.           <?php echo $item['Item']['title']; ?>
  7.         </div>
  8.       <?php endfor; ?>
  9.     </div>
  10.   <?php endwhile; ?>
  11. </div>

This will put 4 items in each row and a smile on your face for not having to write very ugly code to achieve the same : ).

-- Felix Geisendörfer aka the_undefined