Theming blocks individually, by region, or by module

Last modified: August 27, 2009 - 02:50

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}"> 

Note that custom blocks created in the admin UI count as coming from block.module, so their template is 'block-block-[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.

Need a prime block.tpl.php also

dman - November 27, 2008 - 04:47

I found that
block-[module].tpl.php
did not take effect unless there was also a custom (copy of)
block.tpl.php
in the theme dir also. It may have just been Zen template that does odd things with template suggestions.

Also, the module part of the filename is verbatim - using underscores not dashes, so for local_menu.module it's
block-local_menu.tpl.php
which is a little inconsistent, but makes sense.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

you are right

e-roula - July 27, 2009 - 08:12

i also had to copy block.tpl.php from genesis to its sub theme for this to work, i guess this applies to all starter themes.

Thanks for the hint, I

Simsalabim - January 25, 2010 - 21:17

Thanks for the hint, I wondered why it didn´t work. Now everything´s fine. :)

 
 

Drupal is a registered trademark of Dries Buytaert.