Return to Snippet

Revision: 37227
at December 6, 2010 16:15 by focal55


Initial Code
/**
 * Override or insert PHPTemplate variables into the templates.
 */
function phptemplate_preprocess_page(&$vars) {

	// Add unique classes for each page and website section
	$path = drupal_get_path_alias($_GET['q']);

	// append the body classes array with the current path
	$body_classes[] = phptemplate_format_class('page-' . $path);

	// set body_classes var with spaces in between each class
	$vars['body_classes'] = implode(' ', $body_classes);
	if (module_exists('path')) {
		$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
		if ( $alias != $_GET['q'] && $alias == $_REQUEST['q'] ) {
			$suggestions = array();
			$template_filename = 'page';
			foreach (explode('/', $alias) as $path_part) {
				$template_filename = $template_filename . '-' . $path_part;
				$suggestions[] = $template_filename;
			}
		}
		$vars['template_files'] = $suggestions;
	}
}
//Helper Function for formatting css class names
function phptemplate_format_class($string) {
 return strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
}

Initial URL


Initial Description
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.

Initial Title
Drupal 6 Unique Body Classes

Initial Tags
drupal

Initial Language
PHP