1

I have added a div in views header (Custom text) section like this

 <div class="something">  

This div's closing tag is added in views footer section. There are some rows in field section as well. The Display output: template is as below

<?php 
  print $header;
  print $rows; 
  print $footer;
 ?>

So I am expecting the the result as

<div class="something">  /* header*/
    <div>row1</div>      /* In fields */
    <div>row2</div>      /* In fields */
 </div>                  /* In Footer*/

But what I am getting is

 <div class="something"></div>
       <div>row1</div>
       <div>row2</div>

The div in header is automatically closed by drupal 7. How can i avoid that

2 Answers 2

5

Method 1: Unfiltered text

When setting up the header and footer in your view, select Global: Unfiltered text. For the header enter <div class="something"> and for the footer type in </div>.

If you then override the default views-view.tpl.php, stripping out everything except

<?php print $header; ?>
<?php print $rows; ?>
<?php print $footer; ?>

... you should achieve output similar to:

<div class="something">        
  <div class="views-row views-row-1"></div>
  <div class="views-row views-row-2"></div>
</div> 

Method 2: Use attachments

This might be ideal if editing the header and footer through the Views UI isn't important, but you want some flexibility in the output that wraps around your rows; for example, if you wanted it to be dynamic.

Override the default views-view.tpl.php so that it looks like this:

<?php print $attachment_before; ?>
<?php print $rows; ?>
<?php print $attachment_after; ?>

In a custom module you would then implement hook_views_pre_render():

function MODULENAME_views_pre_render(&$view) {
  $header_str = '<div class="something">';
  $footer_str = '</div>';

  if ($view->name == 'your_view_name' && $view->current_display == 'your_display_name') {
    $view->attachment_before = $header_str;
    $view->attachment_after = $footer_str;
  }
}

(Remember to clear the cache after adding the function.)

Method 3: Simple template override (simplest but least flexible)

Override the default views-view.tpl.php and simply replace all of the mark-up with

<div class="something">
<?php print $rows; ?>
</div>

Hope that helps!

A couple of links that might be useful:

0
0

One can also use row classes. These can be set on every views row and will wrap all fields inside then.

This is available under the settings link next to the formatting options.

1
  • Oh, I now see this div should wrap all rows not just one. This might not be helpful then... Commented Aug 8, 2015 at 11:27

Your Answer

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

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