Return to Snippet

Revision: 1865
at November 16, 2006 09:33 by markhope

Initial Code
*  This snippet creates a <body> class and id for each page.
* - Class names are general, applying to a whole section of documents (e.g. admin or ).
* - Id names are unique, applying to a single page.
// Remove any leading and trailing slashes.
$uri_path = trim($_SERVER['REQUEST_URI'], '/');
// Split up the remaining URI into an array, using '/' as delimiter.
$uri_parts = explode('/', $uri_path);

// If the first part is empty, label both id and class 'main'.
if ($uri_parts[0] == '') {
    $body_id = 'main';
    $body_class = 'main';
else {
    // Construct the id name from the full URI, replacing slashes with dashes.
    $body_id = str_replace('/','-', $uri_path);
    // Construct the class name from the first part of the URI only.
    $body_class = $uri_parts[0];
* Add prefixes to create a kind of protective namepace to prevent possible
* conflict with other css selectors.
* - Prefix body ids with "page-"(since we use them to target a specific page).
* - Prefix body classes with "section-"(since we use them to target a whole sections).
$body_id = 'page-'.$body_id;
$body_class = 'section-'.$body_class;
print "<body class="$body_class" id="$body_id"";
print theme('onload_attribute');
print ">";

Initial URL

Initial Description

Initial Title
Drupal body class

Initial Tags
css, class, php, textmate, drupal, body

Initial Language