Drupal Views grid style template with float and divs

Copy and save in a file named views-view-grid.tpl.php in your themes template catalog. Set number of columns in the View. Make the divs float via CSS. Voila!

<?php
/**
 * @file views-view-grid.tpl.php
 * Default simple view template to display a rows in a grid.
 *
 * - $rows contains a nested array of rows. Each row contains an array of
 *   columns.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)) : ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<div class="clearfix"> 
<?php 
  // Check number of columns in grid
  $numberOfColumns = sizeof($rows[0]);
  $columnContainers = array();
 
  // Create containers for all divs
  for ( $iter=0; $iter < $numberOfColumns;  $iter++){
    $columnContainers[] = '<div class="grid-column col-' .$iter. ' clearfix">';
  }
 
  foreach ($rows as $row_number => $columns){
    foreach ( $columns as $column_number => $item ) {
      $columnContainers[$column_number] = $columnContainers[$column_number] . 
                                        '<div class="' . $column_classes[$row_number][$column_number] . '">' . 
                                          $item . 
                                        '</div>';
    }
  }
 
  for ( $iter=0; $iter < $numberOfColumns;  $iter++){
    echo $columnContainers[$iter] . '</div>';
  }     
?>  
</div>

Post a Comment

Your email is never shared. Required fields are marked *

*
*

Auf und ab. (RSS) + Sator-ii theme by Felipe LavĂ­n