Drupal - Views - Add a wrapper DIV around a Group

I needed to do this same thing recently. You can create a template file:

  • Find the template under /modules/views/themes/views-view-unformatted.tpl.php.
  • Copy it into your /sites/all/themes/<your-theme> folder and rename to views-view-unformatted--<nodetype>.tpl.php.
  • Edit the file adding a div around the entire template. The <h3> tag is the group name.

Here is what the modified template file might look like.

<div class="your-class">
<?php if (!empty($title)): ?>
    <h3><?php print $title; ?></h3><!--this is the group name-->
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes_array[$id]; ?>">
    <?php print $row; ?>
</div>
<?php endforeach; ?>
</div><!--end your div-->

Try Format:HTML list. That will wrap the whole thing in an item-list. For my purpose it's perfect (a list of taxonomy items). Hope this helps.


Have you tried the Rewrite results feature? Click on the field you wish to edit and scroll down until you see Rewrite results. Check the box for Rewrite the output of this field and then customize the HTML as necessary. For the tokens, you can use the Replacement Patterns shown in the box below the text input area.

Regarding the Replacement Patterns, note the warning that Views displays:

Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.

If this isn't enough, try adding a new field, Global: Custom text. This will allow you to add some arbitrary HTML and the Replacement Patterns are available here as well. You can use two separate Global: Custom text fields to add the <div> start and end tags.

Tags:

Views