Zen theme

Classes For (Almost) Everything In A Drupal Theme

Thanks to the Zen theme there is an awesome bit of code available to Drupal themers which enables the addition of dynamic classes to your body tag based upon a number of different parameters, such as your position and status within a site, like so:

Drupal theme body classes

As you can imagine, this is incredibly useful for Drupal theming, as it provides a set of classes on the body which then allow a simple way to target specific areas of a theme via CSS.

I used to end up writing lots of different bits of code into a theme file to achieve this functionality, but the Zen theme does a great job of wrapping it all up in one bundle, and also nicely comments the code so that you can figure out what's happening :)

How to use the code:

Implementing the code into your own theme is a simple two step process:

  • Step 1.
    Copy the main chunk of code into your template.php file
  • Step 2.
    Edit your theme's page.tpl.php file to add the body classes

Step 1
The main chunk of code you will need lives in the Zen theme's ZENtemplate.php file.

I have edited it slightly to remove any Zen theme specific code (such as references to Zen theme sub-themes).

Copy all of this code (except the opening and closing php tags) and paste it into your theme's template.php file.

(Just be aware that if your theme's template.php file already contains a _phptemplate_variables() function you will need to integrate the new code with your existing code, as you cannot re-declare the same function. Unfortunately, it's difficult to give a more detailed explanation about how to do this as each situation will be different.)