Posted By

markhope on 11/16/06


Tagged

css class php textmate drupal body


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

panatlantica


Drupal body class


 / Published in: HTML
 

  1. <?php
  2. /**
  3. * This snippet creates a <body> class and id for each page.
  4. *
  5. * - Class names are general, applying to a whole section of documents (e.g. admin or ).
  6. * - Id names are unique, applying to a single page.
  7. */
  8. // Remove any leading and trailing slashes.
  9. $uri_path = trim($_SERVER['REQUEST_URI'], '/');
  10. // Split up the remaining URI into an array, using '/' as delimiter.
  11. $uri_parts = explode('/', $uri_path);
  12.  
  13. // If the first part is empty, label both id and class 'main'.
  14. if ($uri_parts[0] == '') {
  15. $body_id = 'main';
  16. $body_class = 'main';
  17. }
  18. else {
  19. // Construct the id name from the full URI, replacing slashes with dashes.
  20. $body_id = str_replace('/','-', $uri_path);
  21. // Construct the class name from the first part of the URI only.
  22. $body_class = $uri_parts[0];
  23. }
  24. /**
  25. * Add prefixes to create a kind of protective namepace to prevent possible
  26. * conflict with other css selectors.
  27. *
  28. * - Prefix body ids with "page-"(since we use them to target a specific page).
  29. * - Prefix body classes with "section-"(since we use them to target a whole sections).
  30. */
  31. $body_id = 'page-'.$body_id;
  32. $body_class = 'section-'.$body_class;
  33. print "<body class="$body_class" id="$body_id"";
  34. print theme('onload_attribute');
  35. print ">";
  36. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: EvanL on June 15, 2008

I couldn't get this to work. I kept getting the error: Parse error: syntax error, unexpected T_VARIABLE And yes, I made sure that there was a ";" at the end of lines. This would be AWESOME if I could get it to work.

You need to login to post a comment.