Shave a couple of stubborn of DIV-wrappers off your Drupal site
One of the more annoying things about theming Drupal sites is having
to wade through the staggering amounts of wrapping <div>
elements and
containers. Some of these are are fairly easy to get rid of. Others
require you to override core templates.
I recently found a clean way to get rid of a couple of those. These two
were introduced in Drupal 7, and you will probably find them on almost
all Drupal 7 sites – they look like this:
Or in markup:
The culprits 1
2
3
4
5
6
7
<span class="line"><span class="nt"><div</span> <span class="na">class=</span><span class="s">"region region-content"</span><span class="nt">></span></span><span class="line"> <span class="nt"><div</span> <span class="na">id=</span><span class="s">"block-system-main"</span> <span class="na">class=</span><span class="s">"block block-system"</span><span class="nt">></span></span><span class="line"> <span class="nt"><div</span> <span class="na">class=</span><span class="s">"content"</span><span class="nt">></span></span><span class="line"> <span class="c"><!-- Actual page content here --></span></span><span class="line"> <span class="nt"></div></span></span><span class="line"> <span class="nt"></div></span></span><span class="line"><span class="nt"></div></span></span>
Now, the last of these wrappers are actually useful, the rest stems from
one of the changes in Drupal 7, namely that the main page content is now
a block, that can be positioned on the page via Drupal’s block system.
Now, that’s a nice concept, but all the site I’ve seen do business as
usual, and get around this inconvenience by creating a block region
called “content” and sticking the content-block in there as the only
thing, leaving the region and block wrappers as more DIV-spam in your
site’s markup.
So unless you’re actually doing something different with the content
block and/or region, you can just get rid of these extra wrappers by
sticking the two following templates in your theme’s template folder:
(region–content.tpl.php) download1
2
3
4
5
6
7
8
<span class="line"><span class="cp"><?php</span></span><span class="line"><span class="sd">/**</span></span><span class="line"><span class="sd"> * @file</span></span><span class="line"><span class="sd"> * Render the main content block region.</span></span><span class="line"><span class="sd"> *</span></span><span class="line"><span class="sd"> * We don't print all kinds of wrapper divs and titles, just the content.</span></span><span class="line"><span class="sd"> */</span></span><span class="line"><span class="k">print</span> <span class="nv">$content</span><span class="p">;</span></span>
(block–system–main.tpl.php) download1
2
3
4
5
6
7
8
<span class="line"><span class="cp"><?php</span></span><span class="line"><span class="sd">/**</span></span><span class="line"><span class="sd"> * @file</span></span><span class="line"><span class="sd"> * Render the main content block.</span></span><span class="line"><span class="sd"> *</span></span><span class="line"><span class="sd"> * We don't print all kinds of wrapper divs and titles, just the content.</span></span><span class="line"><span class="sd"> */</span></span><span class="line"><span class="k">print</span> <span class="nv">$content</span><span class="p">;</span></span>
Short and sweet :)