Drupal - How can I remove the default generated <div> tags in view's result?

The easiest thing to do is copy the template files from "sites/all/modules/views/theme/" into you theme (I normally make a "views/" subdirectory).

If you want to make this change for all themes, edit "views-view-unformatted.tpl.php".

If you want to just change that view, edit the view then click on the Theme: Information link. It will show the template suggestions for the view. Copy If you want to make this change for all themes, copy "views-view-unformatted.tpl.php" to the proper name, and edit that file.

In both cases, make sure you clear the Drupal caches to make sure the system finds the new files.


If you don't want to touch the theme you could use Semantic Views module to strip all the extra div's.


This solution combines and builds on David Paul's and MDP's solutions and removes the <div class="views-row ..."> in a consistent way for all views that are properly configured without additional modules.

Drupal 7 & 8

Step 1: Disable all class configurations in the Style options of the view's Format section.

You must do this for any and all views that you want to remove the wrapper from:

Views Format Style Settings

Drupal 7

Step 2: Copy views-view-unformatted.tpl.php from the Views module into your theme's templates folder and modify the logic to ignore the div when classes are empty.

Change the following lines from:

<div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
  <?php print $row; ?>
</div>

to:

<?php if ($classes_array[$id]) : ?><div class="<?php print $classes_array[$id]; ?>"><?php endif; ?>
  <?php print $row; ?>
<?php if ($classes_array[$id]) : ?></div><?php endif; ?>

Drupal 8

Step 2: Copy views-view-unformatted.html.twig from the Views module into your theme's templates/views/ folder and modify the logic to ignore the div when {{row.attributes}} are empty.

Change the following lines from:

<div{{ row.attributes.addClass(row_classes) }}>
  {{ row.content }}
</div>

to:

{% if default_row_class or row.attributes is not empty %}
<div{{ row.attributes.addClass(row_classes) }}>
  {{ row.content }}
</div>
{% else %}
  {{ row.content }}
{% endif %}

Honestly, I'm not sure why the Views module doesn't do this itself... its a total WTF and could be a nice improvement for Themer's eXperience.

Tags:

Views