We Recommend

HTML: The Definitive Guide HTML: The Definitive Guide
They teach you that learning HTML is like learning any other language and that reading a book of rules can only take you so far. Readers begin writing what may be their first Web page just two pages into the book's second chapter. From there on, they provide a wide range of HTML coding to allow readers to learn from good examples. The book includes a handy "cheat sheet" of HTML codes for quick reference.


Ballyhoo


Posted By

markhope on 11/16/06


Tagged

css class php textmate drupal body


Versions (?)


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 

You need to login to post a comment.