Skip to Content

block.tpl.php Overview

| |

block.tpl.php Basics

This is the template file for blocks. Blocks are small containers for content that are placed throughout a drupal layout, where ever there are block regions.

Before Drupal 4.7, blocks could only show in the left and/or right sidebars. Most themes have four or five regions:

  • header
  • left
  • right
  • footer

theme_block function

The theme_block function is like theme_page and theme_node. The theme_block function returns a themed block. The function contains the mark-up that is generated by default. This code is very short and easy to read:

function theme_block($block) {
  $output  = "<div class=\"block block-$block->module\" id=\"block-$block->module-$block->delta\">\n";
  $output .= " <h2 class=\"title\">$block->subject</h2>\n";
  $output .= " <div class=\"content\">$block->content</div>\n";
  $output .= "</div>\n";
  return $output;
}

We can easily convert the output of the function into HTML ourselves:

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

Above you see the output converted into HTML with embedded PHP

Dynamic IDs an CLASSes

The following snippets of code take from above are very interesting. This line contains the class values for the containing DIV:

<?php print "block block-$block->module" ?>

Every block has a class value of "block" and another value that is "block-" plus the name of the module that generated the block, i.e. "block-user"

This next line contains the ID values for the same containing DIV:

<?php print "block-$block->module-$block->delta"; ?>

Every block also has a unique ID value which is a combination or concatenation of "block-" + name of the module + "-" + delta, i.e. "block-user-1". Delta being the number of the block, in the module.

You can use this combination of general CLASSes and unique IDs to achieve very sophisticated style results.