11

I am using a views in my drupal web site. I have set 'style' to 'Unformatted' and 'Row style' to 'Fields'

Also I have Rewrote the output of the field with my HTML tags.

But I have a problem in the result. Every row is located inside a [div] tag for example the first row is located in

<div class="views-row views-row-1 views-row-odd views-row-first"></div>

How can I setup the view in order to remove this extra [div] tag ?!?!?

I don't need this wrapper.

7 Answers 7

7

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

6
  • Does the Drupal 7 version work well?
    – mpdonadio
    Commented Apr 29, 2012 at 18:06
  • 1
    @MPD it does on my site :) Commented Apr 29, 2012 at 21:03
  • @nathan6137, thanks. I have been hesitant to use it on a client site b/c there hasn't been activity of the project in a while.
    – mpdonadio
    Commented Apr 30, 2012 at 13:17
  • Semantic Views is not necessary if you are using Views 3.x - the functionality of SV is wrapped into Views core. drupal.org/node/740686
    – sheena_d
    Commented Apr 30, 2012 at 13:42
  • @sheena_d, did all of the SV functioality get folded in? I thought it was just fields and that you couldn't pick elements for things like how rows in an unformatted list get generated.
    – mpdonadio
    Commented Apr 30, 2012 at 16:22
9

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.

8

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.

5
  • 4
    THIS is the correct answer
    – ajmedway
    Commented Jan 4, 2018 at 10:10
  • 1
    I've added instructions now for Drupal 8 to this answer. Commented Mar 31, 2018 at 3:21
  • 1
    Should be the accepted answer in my opinion. Commented Apr 29, 2019 at 10:45
  • would be even better if the row-class could be added to the row.content without creating that extra div at all Commented Jul 10, 2020 at 7:44
  • Not sure I understand how that would work. This is literally the only wrapper you get around the views results, the only other wrappers are at the individual row level. Commented Jul 10, 2020 at 13:19
1

You want to look into the Theming: Information section and override one of the styled templates for your view. That's where the stuff happens. Be sure to use the Rescan button when you create an appropriately named tpl file, so that Drupal picks it up.

1

To remove this in Drupal 7 you can click on the Fields settings and turn off "Provide default wrapper elements"

enter image description here

0

If you're on Drupal 7 then you don't need to play with template files to remove the wrapper divs. Those wrappers come as part of the output, which suggests they can be controlled in the view, which they can.

Under FORMAT > Show: fields | settings - click settings, then uncheck the wrapper option. That's it.

1
  • 1
    that is not entirely true, even if you uncheck wrapper options, views would not let you remove all divs
    – loparr
    Commented Mar 18, 2013 at 11:10
0

I have achieved this by overriding views-view.tpl.php file. Copy this file from views module into your theme directory and remove the beginning wrapper. enter image description here

Also remove the div wrapping the $rows.

enter image description here

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.