0

Hi there I am trying to use a views template override to put a wrapper div around every group of two rows. If the last group only has 1 row ( say if there were 5 rows total ) I want to end the wrapper div.

I found some code for this but it's not working I am getting zero output from this I can see my comment tag but that it.

<!-- test row output -->

<?php 
$group_nr = 1;                  // first group number
$last_row = count($rows) -1;    // last row
$wrapper  = 2;                  // put a wrapper around every 2 rows
?>
<?php foreach ($rows as $id => $row): ?>
<?php if ($id % $wrapper == 0) {print '<div class="row">'; $i = 0; $group_nr++; } ?>
<?php print $row; ?>
<?php $i++; if ($i == $wrapper || $id == $last_row) print '</div>'; ?>
<?php endforeach; ?>

Any idea what I am doing wrong

My template is called views-view--homepage-awards--block-2.tpl.php

I want my output to look like this

<div class="row">
  <div>row1</div>
  <div>row2</div>
</div>
<div class="row">
  <div>row3</div>
  <div>row4</div>
</div>
<div class="row">
 <div>row5</div>
</div>

Any help would be greatly appreciated.

1
  • I am not saying this is not possible, but I am curious, what is your end goal? What are you trying to accomplish by wrapping the rows this way? Mar 31, 2014 at 0:46

1 Answer 1

4

This should accomplish it:

<?php
  $last=count($rows) - 1;
?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!($id & 1)): // if this an even row id, open the <div> ?>
    <div class="row">
  <?php endif; ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
  <?php if ($id==$last || ($id & 1)): // if this the last or an odd row id, close the <div> ?>
    </div>
  <?php endif; ?>
<?php endforeach; ?>

It'll wrap each group of two rows with a div with a class of row and make sure if it's an odd number that the final div is closed.

Now, if you want to remove all the classes from those inner rows that Views inserts, then you just need to clean them up or out of that obvious if in the middle.

CAVEAT: If you have done anything with Views pre_render()ing or other similar hooks that have deleted any of the rows out of the results and not reset the ids to be consecutive, this logic will most probably fail.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.