Getting the classes to print

terramedia - December 2, 2008 - 03:35
Project:Themer
Version:6.x-2.0-3
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

It seems to be just me since there is no sign of this issue anywhere else, but I can't get the classes to print, is there anything I need to put in template.php or in page.tpl.php to get it to print them or should that all be fine without changing anything? At the moment in the body tag in page.tpl.php I have the fairly standard:

<body<?php print phptemplate_body_class($left, $ight); ?>>

Which I need to keep, do I need to make any changes to this?

#1

tjholowaychuk - December 3, 2008 - 16:56

That may be an issue. Which theme are you using? If the theme is 'greedy' and prints out class="", this is a bad practice, as now you cannot manually add more such as class="

<?php
print phptemplate_body_class()
?>
my-special-class", however you would need to use themer_body_class() which returns a string of joined classes, which Ideally would be in the page preprocess hook so you could just place
<?php
$body_class
?>
or something... basically they should not have assumed that their func would be the only one printing classes

#2

Todd Nienkerk - May 8, 2009 - 14:57

I think it's worthwhile to utilize the power of <a href="http://api.drupal.org/api/function/template_preprocess_page">template_preprocess_page()</a> to add classes to the body instead of creating separate function in this module.

Drupal 6.x core creates an array of body classes in its template_preprocess_page() implementation; since preprocess functions work at both the module and theme levels, it makes sense to simply tap into that. Why duplicate the work and ignore Drupal's default output? :)

Here's an example:

function THEME-OR-MODULE-NAME_preprocess_page(&$variables) {
  /*
   * Add more body classes
   */
  $body_classes = array($variables['body_classes']);

  // Show/hide admin area using CSS
  if ($variables['is_admin']) {
    $body_classes[] = 'admin-panel';
  }

  // Add new body classes to existing variable
  $variables['body_classes'] = implode(' ', $body_classes);
}

Of course, page.tpl.php needs to print the $body_classes var passed to it from this function:

<body class="<?php print $body_classes; ?>">

 
 

Drupal is a registered trademark of Dries Buytaert.