Summary

This is a really simple snippet that allows you to have more controll over of a page's styling needs.

Usage

For example, let's say you want to assign a background color to specific pages - or - a different image per page (think rotating header images) somewhere on the page. This snippet will allow you to control that more easily and even give greater control traversing even deeper into the xhtml.

How it works

The snippet first looks to see if pathauto is installed. If so it does a query to the url_alias table to see if an alias has been created yet. If no result then it does a quick string replace to make a friendly name for an id or class.

The Snippet

Place the following at the top inside your page.tpl.php file:

<?php
 
if(module_exists('pathauto')) {
   
$result = db_query("SELECT dst FROM {url_alias} WHERE src='%s'", $_GET['q']);
    if (
$data = db_fetch_object($result)) {
     
$body_id = str_replace("/", "-", $data->dst);
    }
  }
  else {
     
$body_id = str_replace(array('/', '_'), array('-', '-'), $_GET['q']);
  }
?>

Now change your body tag:

From:

<body>

To:

<body id="<?php print $body_id; ?>">

If you know of other uses for this snippet OR want to improve on it, feel free to do it in the comment form below.

Happy Styling!

Eric Carroll

07.03.2008

Great work, Elvis (as usual).

How would you go about getting an id on the front page and on a view? I've tried this on a view I have, but it leaves the blank (id="").

Eric Carroll

07.03.2008

I mean to say it leaves the body id blank.

elvis

07.03.2008

@Eric, Ah, I will have to modify the snippet to pick that up... Let me look into that a bit later.

JW

08.08.2008

You should be putting that logic into template.php instead of page.tpl.php

elvis

08.08.2008

@jw, Just depends. If someone has several page-type.tpl.php files, yes. For simple sites, no need to make things more complicated. I honestly had no plans on providing this as a snippet, I didn't put much thought into it. Only my friend suggested I should mention it at the least - it saved him hours of css work.

elvis

08.08.2008

@jw, Just depends. If someone has several page-type.tpl.php files, yes. For simple sites, no need to make things more complicated. I honestly had no plans on providing this as a snippet, I didn't put much thought into it. Only my friend suggested I should mention it at the least - it saved him hours of css work.

Eric Carroll

08.08.2008

Yes, this worked great for the newest version of my company site. I did modify it a bit by adding another else statement that changes the ID to 'work' if it is the work page view (the portfolio page).

Name
E-mail (Kept Private)
CAPTCHA
Are you human?
Image CAPTCHA