Alternative templates for different block types (4.7)

Last modified: July 22, 2007 - 22:00

Note: Drupal 5 makes this even cleaner. Just create a properly named template file

Method 1

Here's a little trick that will allow you to use a different block.tpl.php for a specific block. You can control by Block name or Block ID. Here' a modified block.tpl.php with a conditional at the top. All you need to do is edit the module == '[modulename here]' and delta == '[block name or id here]' part.

Below is my current block.tpl.php, and the block information in the conditional is for my site. You will need to change it as described above for it to work on your site. This is for loading a custom block.tpl.php for two blocks on my site, but you could use it for one, six or however many you need. You will need a conditional (the "if " part) for each custom template.

<?php
if ( ( $block->module == 'views' && $block->delta == 'Cool Block' ) || ( $block->module == 'node' && $block->delta == '0' ) ) {
    include
'block-custom.tpl.php'; /*load a custom template for those two block IDs */
   
return; }
?>

<div class="<?php print "block block-$block->module" ?>" id="<?php print "block-$block->module-$block->delta"; ?>">
  <div class="title"><h3><?php print $block->subject ?></h3></div>
  <div class="content"><?php print $block->content ?></div>
</div>

You need to create another block-custom.tpl.php (or block-whateveryoulikehere.tpl.php) and you can have as many of these as you like. To test this on your site, just duplicate the standard block template, name it block-custom.tpl.php, and edit it, putting the title last instead of first and you'll see the effect. The rest is up to you. You could put whatever you want in the template.

This means all blocks are NOT created alike, which is something that drives many people crazy. Add this to the new regions functionality and you have 100% customizable blocks.

Credit to iDonny, nevets and Heine

Method 2

by JohnAlbin

The contents of block.tpl.php:

<?php
    $block_module
= $block->module;
   
// force delta to be alphanumeric
   
$block_name = $block_module . '-' . preg_replace('/[^\w]/', '', $block->delta);
    switch (
$block_name) {
        case
'menu-2':                // Primary links
       
case 'views-CoolBlock':        // a custom Views block named "Cool Block"
           
if ( @include("block-$block_name.tpl.php") ) {
                return;
            }
            break;
    }
?>

<div class="block block-<?php print $block_module; ?>" id="block-<?php print $block_name; ?>">
<h2 class="title"><?php print $block->subject; ?></h2>
<div class="content"><?php print $block->content; ?></div>
</div>

This code also has the following advantages:

1. id attributes won't have weird characters in them (like spaces).
2. A missing "block-[module]-[delta].tpl.php" file won't cause a PHP warning and will fallback on the standard block.tpl.php code.

Method 3

by dman

At the very top of block.tpl.php:

<?php
   
// allow for unique blocks to have their own phptemplate theme
   
if(!empty($block->subject))
     
$blockid = 'block-'.strtolower(preg_replace("/[^A-Za-z0-9]+/","-",$block->subject));
    else
     
$blockid = 'block-'.$block->module.'-'.$block->delta;

    if (
file_exists(path_to_theme() . "/$blockid.tpl.php")) {
      include
path_to_theme() . "/$blockid.tpl.php";
      return;
    }
?>

Now you can impliment block-block-7.tpl.php or block-login.tpl.php, etc.

 
 

Drupal is a registered trademark of Dries Buytaert.