2

I have a views block that is displayed in the content section of a node. What I need is wrapping the rows of the views block by divs. I have a list of 10 divs like so:

<div class="row-1">Some Text here</div>
<div class="row-2">Some Text here</div>
<div class="row-3">Some Text here</div>
<div class="row-4">Some Text here</div>
<div class="row-5">Some Text here</div>
<div class="row-6">Some Text here</div>
<div class="row-7">Some Text here</div>
<div class="row-8">Some Text here</div>
<div class="row-9">Some Text here</div>
<div class="row-10">Some Text here</div>

Now I want to wrap the first 3 divs, div 4 to 8 and div 9 and 10 in a separate wrapper div like so.

<div class="wrapper-1">
<div class="row-1">Some Text here</div>
<div class="row-2">Some Text here</div>
<div class="row-3">Some Text here</div>
</div>
<div class="wrapper-2">
<div class="row-4">Some Text here</div>
<div class="row-5">Some Text here</div>
<div class="row-6">Some Text here</div>
<div class="row-7">Some Text here</div>
<div class="row-8">Some Text here</div>
</div>
<div class="wrapper-3">
<div class="row-9">Some Text here</div>
<div class="row-10">Some Text here</div>
</div>

How would I edit my views.tpl.php? Thanks!

3
  • Stack Exchange sites are not forums: Answers are just for answering the question, not to ask further information to who answered your question. For that, there are comments, which you can use for answers given to your questions. – kiamlaluno Nov 9 '12 at 18:42
  • This thread got too rich in comments and misunderstandings, mostly due to beginner's level of the asker, also in terms of php itself. I still think the final answer might help someone using Views. My vote would be to delete all the ramblings, then I paste the final answer that worked for criscom and then criscom accepts it. – Artur Nov 9 '12 at 21:35
  • Ok. See bottom for what worked for me. – criscom Nov 18 '12 at 11:15
1

Here is what worked for me. Mind that I only got it to work in an unformatted list format in views.

<?php
  $currIx = 1; // This will be our incremental index variable. Let's make it 1-based to maintin consistency with your example.
  $currWrapperIx = 1; // Wrapper counter
  $wasOpen = FALSE; // This variable will be used later as a flag to watch for properly closing wrapper divs.
?>

<?php if (!empty($title)): ?>
  <p class="<?php print $title ?>"><?php print $title; ?></p>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php switch ($currIx){
    // List the row indices that are supposed to begin a new wrapper.
    case 1:
    case 27:
      if($wasOpen == TRUE){ // Close current wrapper div before opening a new one.
        print "</div>\n";
        $wasOpen = FALSE;
        $currWrapperIx ++; // Increment the index for next wrapper's class.
      }
      // Open a new wrapper div
      print "<div class=\"wrapper-{$currWrapperIx}\">\n";
      $wasOpen = TRUE;
      break;

    default:
      // For other row indices do nothing.
    }
  ?>
<!--<div <?php if ($classes_array[$id]) { print 'class="' . $classes_array[$id] .'"';  } ?>>-->
  <div class="<?php print $id ?>"><?php print $row; ?></div>
<!-- </div>-->

  <?php $currIx ++; ?>
<?php endforeach; ?>

<?php
    // Close the last wrapper.
    if($wasOpen == TRUE){
      print "</div>\n";
      $wasOpen = FALSE;
    }
?>

Kudos go out to @Artur. Great support. Many thanks.

1
  • paste that into your views-view-unformatted--default.tpl.php and put it into your template folder in your theme. – criscom Nov 8 '12 at 22:32
0

Sure thing criscom. Below is a very rudimentary piece of code.

<?php
  $rows = array(
    1 => "row1",
    2 => "row2",
    3 => "row3",
    4 => "row4",
    5 => "row5",
    6 => "row6",
    7 => "row7",
    8 => "row8",
    9 => "row9",
    10 => "row10",
  );
?>

<?php
  $currIx = 1; // This will be our incremental index variable. Let's make it 1-based to maintin consistency with your example.
  $currWrapperIx = 1; // Wrapper counter
  $wasOpen = FALSE; // This variable will be used later as a flag to watch for properly closing wrapper divs.
?>

<?php foreach ($rows as $id => $row): ?>
  <?php switch ($currIx){
    // List the row indices that are supposed to begin a new wrapper.
    case 1:
    case 4:
    case 9:
      if($wasOpen == TRUE){ // Close current wrapper div before opening a new one.
        print "</div>\n";
        $wasOpen = FALSE;
        $currWrapperIx ++; // Increment the index for next wrapper's class.
      }
      // Open a new wrapper div
      print "<div class=\"wrapper-{$currWrapperIx}\">\n";
      $wasOpen = TRUE;
      break;

    default:
      // For other row indices do nothing.
    }
  ?>

  <div><?php print $row; ?></div>

  <?php $currIx ++; ?>
<?php endforeach; ?>

<?php
    // Close the last wrapper.
    if($wasOpen == TRUE){
      print "</div>\n";
      $wasOpen = FALSE;
    }
?>

Sorry, I did not bother to make it pretty-print, but the source of the generated page has the div structure you asked for. The code is abstract and has dummy rows array, so you can test it by simply pasting it in an empty PHP file and trigger it outside Drupal. Anyway you should get the idea.

Update

You used the PHP code from view-view.tpl.php. This is a template file with a broader picture on a view's output structure. In this file you don't have any control over the traversal (the foreach loop). It just says print $rows and that's it, meaning "print out the loop, whatever it looks like." What you need is to specify how the loop is generated - that is: open your view, open the Theming information and pick one of the tpl in the Style group (where tpl's contain the word unformatted). There you'll be able to mess with the loop. Remove my code from the former file and move it to that file. Rescan templates.

8
  • Thanks a lot for that code, Artur. I have implemented above code into my view-view-list--block-1.tpl.php. I suspect that the tpl.php file I am using is the wrong one since the divs are inserted too low down in the html. I get something like this: <div class="row-1>Some text<div wrapper-1>...</div></div> But thanks a lot. This gives me something to play with. I just have to get in a little higher. – criscom Nov 8 '12 at 20:00
  • Quite a chestnut to get it to work in my views, though it works great when i run it outside drupal. – criscom Nov 8 '12 at 20:13
  • when I add the code to views-view--block-1.tpl.php I get an error message: Warning: Invalid argument supplied for foreach() in include() (line 64 of views-view--block-1.tpl.php line 64 is: <?php foreach ($rows as $id => $row): ?> – criscom Nov 8 '12 at 20:36
  • I'm sorry this is causing you so much trouble, it was meant to be of some help. I'm not sure why it wouldn't work. The code is based on the generic Style view tpl (/views/theme/views-view-unformatted.tpl.php). I haven't tested it within Drupal, but if you comment out the initial $rows array, views should use the real rows. Feel free to PM me or paste the entire tpl code you're using. – Artur Nov 8 '12 at 20:42
  • Hey nothing to be sorry about. Your code taught me a lot to say the least. Very much appreciate your help on this. – criscom Nov 8 '12 at 20:53

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