Create more readable (and portable) block id attributes based on the block title
Going crazy with a style sheet full of impossible to remember #ids? How about trying to share or reuse styles between sites where the id's aren't the same? We can use the "Block title" field to create friendlier id names like #address instead of #block-block-24.
block.tpl.php
<?php
// Create css id attribute based on the 'Block title' field if available.
if (!empty($block->subject)) {
// Make lowercase.
$blockid = strtolower($block->subject);
// Convert special characters to dashes.
$blockid = preg_replace("/([^a-z0-9])/", "-", $blockid);
}
else {
// If no "Block title", create css id attribute the robotic way.
$blockid = "block-$block->module-$block->delta";
}
?>
<div class="block<?php print $block->module ? ' block-'.$block->module : '' ?>" id="<?php print $blockid ?>">
<h2><?php print $block->subject ?></h2>
<div class="content"><?php print $block->content ?></div>
</div>The regex and string replacements do a basic job of enforcing legal CSS names. If you're disciplined, you remove them altogether and save some overhead, but this would really limit your block titles assuming you ever display them.
One small thing to should remember when naming your blocks is that in CSS ids can't start with a number — something this particular snippet doesn't account for.
