Sometimes I have wanted to make some simple changes to a drupal theme based on the current root path. The sections module does this, but I wanted a simpler way of changing elements in the CSS based on the current root path.
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'page':
if (module_exists('path')) {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
foreach (explode('/', $alias) as $path_part) {
$classes[] = $path_part;
}
}
//because locale will add the country code as the first argument
if (module_exists('locale')) {
$vars['body_class'] = $classes[1];
} else {
$vars['body_class'] = $classes[0];
}
}
}
return $vars;
}
When this code is added to a template.php file it creates a new template variable called body_class that you can add to your page.tpl.php file by doing:
<php print $body_class ?>
In the body tag.
Now if I have a site with 2 root paths like blog/test and about/contact the body class will be set to either blog or about. This means I can change add something like this to my CSS:
.blog {
background-color:red;
}
.about {
background-color:blue;
}
On my current project I have created a CSS file called sections.css that can use to declare all the different elements for each section. Because the body tag has the section class, it will overwrite every other declaration of that class in other CSS files.