Put classes around your body


/ Published in: PHP
Save to your folder(s)

Put classes around your bodyy, to allow different colors for seperate pages and areas on your site.
IT will simply add a css-safe version of the first argument in an url. Say the url is /about_us/our_company the class will be
body.about-us
Requires phptemplate


Copy this code and paste it in your HTML
  1. /**
  2. * Make additional variables available and override some core variables
  3. */
  4. function _phptemplate_variables($hook, $vars) {
  5. switch ($hook) {
  6. case 'page':
  7. $vars['page_class'] = (($vars['is_front']) ? 'front' : _MyTheme_css_safe(arg(0)) );
  8.  
  9. if (in_array($vars['page_class'], _MyTheme_list_top_level_classes())) {
  10. $vars['has_menu_image'] = TRUE;
  11. }
  12. break;
  13. }
  14.  
  15. return $vars;
  16. }
  17.  
  18. /**
  19. * Prepare the specified string for use as a CSS identifier.
  20. */
  21. function _MyTheme_css_safe($string, $length = 36) {
  22. // Replace or drop apostrophes based on user settings
  23. $separator = '-';
  24.  
  25. // Preserve alphanumerics, everything else becomes a separator
  26. $pattern = '/[^a-zA-Z0-9]+/ ';
  27. $output = preg_replace($pattern, $separator, $string);
  28.  
  29. // Trim any leading or trailing separators (note the need to
  30. // escape the separator if and only if it is not alphanumeric)
  31. if ($separator) {
  32. if (ctype_alnum($separator)) {
  33. $seppattern = $separator;
  34. }
  35. else {
  36. $seppattern = '\\'. $separator;
  37. }
  38. $output = preg_replace("/^$seppattern+|$seppattern+$/", "", $output);
  39. }
  40.  
  41. // Enforce the maximum component length
  42. $output = drupal_substr($output, 0, $length);
  43.  
  44. return $output;
  45. }
  46.  
  47. /**
  48. * List with toplevel classes
  49. * @TODO: you probably want to make this configurable somewhere;
  50. **/
  51. function _MyTheme_list_top_level_classes() {
  52. $list = array(
  53. 'node',
  54. 'taxonomy',
  55. 'user',
  56. 'profile',
  57. );
  58. return $list;
  59. }
  60.  
  61. /**
  62. * The following takes place in page.tpl.php between 11PM and 12PM;
  63. **/
  64. <?php
  65. //....
  66. <body class="<?php print $page_class ?>">
  67. //....
  68. <?php if (!$is_front && $has_menu_image): ?>
  69. <h2 class="content title" id="content-title-image"><?php print $title ?></h2>
  70. <?php endif; ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.