Setting Blocktheme Classes with subtheme_preprocess_block

I've become a big fan of the fantastic Blocktheme module. Up until now I've followed the README instructions and either built blocktheme templates or just printed the $blocktheme variable in my block.tpl.php and block-xxx.tpl.php templates.

Thats all well and good, but in my current project I wanted to be able to theme blockthemes by region. The main reason for this is we are making just very minor changes to the block styles depending on the region and we see no point in creating additional blockthemes.

Of course I am using the Genesis starter theme for Drupal 6 and Genesis already sets a region class on blocks, but chaining selectors can be problematic for IE6, such as using chains in the form of: {...}

IE6, bless its heart, will only the last class i.e. .my-block-theme-class, so that's not so useful and makes things more of a hassle than they really need to be.

One answer is to build a new class using the block region and blocktheme classes - e.g. something like...

.content-bottom_my-block-theme-class {...}

With this class I can safely target blocktheme blocks by region and not have to worry about IE6.

I could write a bunch of code and dump it in my block.tpl.php file but Genesis already builds an array of classes and prints them using the $block_classes variable - all we need to do is add to that array in function subtheme_preprocess_block.

Open up your Genesis sub themes template.php file and locate the function and modify it to look like this:

function genesis_SUBTHEME_preprocess_block(&$vars, $hook) {
// add special classes to support blocktheme module.
if (module_exists('blocktheme') && isset($vars['blocktheme'])) {
$block_classes[] = $vars['blocktheme'];
$block_classes[] = $block->region.'-'.$vars['blocktheme'];

An alternative approach could be to print the $blocktheme variable on the .block-inner wrapper div - that would work just fine also. I like the preprocess function method because I don't need touch any templates to achieve it.

Some additional interesting reading:

CSS selectors: basic browser support
W3C - Class Selectors

Happy theming, or is that themeing, who knows eh?

Last updated 11th April, 2009 - 8:48pm

Authored by Jeff Burnz on