Posted By

focal55 on 12/06/10


Tagged

drupal templatephp


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Onfire60


Drupal 6 Unique Body Classes


 / Published in: PHP
 

Automatically add a class to the body tag on each page. Remember to add <?php> to your body tag in the page.tpl.php file. You can also override or insert PHPTemplate variables into the templates with this function.

  1. /**
  2.  * Override or insert PHPTemplate variables into the templates.
  3.  */
  4. function phptemplate_preprocess_page(&$vars) {
  5.  
  6. // Add unique classes for each page and website section
  7. $path = drupal_get_path_alias($_GET['q']);
  8.  
  9. // append the body classes array with the current path
  10. $body_classes[] = phptemplate_format_class('page-' . $path);
  11.  
  12. // set body_classes var with spaces in between each class
  13. $vars['body_classes'] = implode(' ', $body_classes);
  14. if (module_exists('path')) {
  15. $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
  16. if ( $alias != $_GET['q'] && $alias == $_REQUEST['q'] ) {
  17. $suggestions = array();
  18. $template_filename = 'page';
  19. foreach (explode('/', $alias) as $path_part) {
  20. $template_filename = $template_filename . '-' . $path_part;
  21. $suggestions[] = $template_filename;
  22. }
  23. }
  24. $vars['template_files'] = $suggestions;
  25. }
  26. }
  27. //Helper Function for formatting css class names
  28. function phptemplate_format_class($string) {
  29. return strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  30. }

Report this snippet  

You need to login to post a comment.