Drupal provides different ways to theme all the blocks of a site at different levels. We can create our own custom html layouts for all the different blocks on a drupal site depending upon the region they belong to, the module that created the block or even as an individual block with your own html and css.
Mostly for all themes that we use for a drupal site has a block.tpl.php file in that particular theme folder. If not, create a block.tpl.php file in the theme folder. A standard block.tpl.php file has the following contents to the minimum requirement.
// $Id: block.tpl.php,v 1.3 2007/08/07 08:39:36 goba Exp $
<div id=”block-<?php print $block->module .’-‘. $block->delta; ?>” class=”clear-block block block-<?php print $block->module ?>”>
<?php if (!empty($block->subject)): ?>
<h2><?php print $block->subject ?></h2>
<div class=”content”><?php print $block->content ?></div>
Following are some ways we can create/edit a template file to play with desigs as well as functionality of blocks in Drupal according to our own desire and requirements.
- Overriding/Styling all the Blocks in the Drupal Site at once:
- Create one block.tpl.php file in the theme folder with the content written above, and now if you want some customization to be applied to all the blocks in the drupal site, this is place. Here you can rearrange the tags, classes, add ids to the pre-existing elements, alter css etc. You can also add or remove elements and add your own custom elements and that will affect all the blocks in the Drupal site.
- Style and theme blocks by region in the drupal sites :
- There are atleast five regions in drupal(6.x) where you can arrange your blocks, the regions are – Header, Footer, Content, Rightside-bar and Leftside-bar. What if you want your footer block should look different from your header block, or the background color of your leftside bar blocks should be different from right side bar. Simple, we need to customize block tpl files on region basis. You need to create a tpl file for a specific region like this : block-regionname.tpl.php. So if the region was footer, then you would create a file called block-footer.tpl.php in the theme folder and you are ready to customize designs that would be specific for all the blocks in footer region.
- Style/Theme individual blocks in the drupal sites:
- To style individual blocks in drupal follows the same pattern like creating the tpl file and overriding the design. But getting the machine readable name for a particular block is little tricky. You need to go to the block page (admin/build/block), find the particular block whose design you want to alter, hover your mouse pointer to that block, it will show you the exact path of that block, just node down the last two items of the url. For example if the url is : admin/build/block/configure/abcd/0 then you just need is abcd and 0. Using this you can now pinpoint that block for an override by creating a tpl file called block-abcd-0.tpl.php.
- Style/Theme a blocks produced by a module:
- To customize the blocks created by a module we need to follow the same steps that we deed in last section (theming individual blocks) however we omit the last item from the file name. So taking the same example the file name in this case will be block-abcd.tpl.php. Customizing things here will affect all the blocks created by abcd module.
NOTES : Its a better practice to keep an original copy of block.tpl.php file extra to the files mentioned in all the sections above in the theme folder.