Theming blocks individually, by region, or by module

In Drupal 5.0, designers can create multiple tpl.php files for blocks based on the specific block, the module that created the block, or the region that the block appears in.

Template files are searched in the following order:

  • block-[module]-[delta].tpl.php
  • block-[module].tpl.php
  • block-[region].tpl.php
  • block.tpl.php

For example, the user login block has a delta of '0'. If you put it in the left sidebar, when it's rendered, PHPTemplate will search for the following templates in descending order:

  • block-user-0.tpl.php
  • block-user.tpl.php
  • block-left.tpl.php
  • block.tpl.php

You can find the block's module and delta by looking at the html source of a page: each block's main DIV has the following classes and IDs:

<div class="block block-{module}" id="block-{module}-{delta}"> 

Styling with CSS

These classes and IDs also let you apply CSS rules to blocks selectively: all blocks, all blocks from a particular module, or just one block. (To style blocks by region, use the DIV that wraps an entire region in your selector.)

Note that the delta is usually a number: each module numbers the blocks it provides, starting at 0. In some cases, the delta can also be a name: eg to style blocks created with the Views module, use block-views-[name of your block].tpl.php.

 
 

Drupal is a registered trademark of Dries Buytaert.