Often, when creating themes in Drupal, I find myself asking "is there a better way to add this <body> class or <h1> tag (and so on...) to the page?" Now while this seems trivial, and there are certainly many ways to go about tackling this exact issue, I thought I'd share my new past method of choice.
Drupal's drupal_get_path_alias() is a perfect an adequate tool for this job.
It is probably better to check your content types using Drupal's $bodyClasses var, it's more reliable as it is not URI based. I have left this example up to act as an example of how one _could_ use drupal_get_path_alias()
Before we start, you should have pathauto installed and configured, "clean urls" enabled, as well as a few content types set up.
To keep things simple, let's assume that you have two content types: "blog" and "static", where "blog" would naturally be your blog, and "static" would be your static pages like "about me" or something similar. Let us also assume you have set up Pathauto to auto alias all node types of "blog" to a path of http://your-site.com/blog/[title-raw] and all node types of "static" to something similar... moving forward.
$alias = drupal_get_path_alias($_GET['q']); print $alias;
If you were to throw this somewhere in the <body> of your page.tpl.php you would see that this prints out your current page's path ( very similar to $_SERVER['REQUEST_URI'] ). Since Drupal passes node information via GET we can pass $_GET['q'] to drupal_get_path_alias() and let it do the lifting for us and trace it's way back to the node in question and returning the stored alias.
Next we use explode() to convert the string $alias into an array.
$path_chunk = explode('/', $alias);
So now that we've gotten to this point we should discuss where this code should actually go. The short answer is in your themes template.tpl.php. If it doesn't exist yet, create a blank file in your theme directory (typically /sites/all/themes/your-theme-name/template.tpl.php).
Now, placing arbitrary code in your template.tpl.php file will get you nowhere. We are going to wrap this code in a preprocess function (if you are unfamiliar with preprocess functions, you should do some research, as this subject is way beyond the scope of this post, find some good resources here... and here ). Our result should look something like this.
function your_theme_name_preprocess(&$vars){ $alias = drupal_get_path_alias($_GET['q']); $path_chunk = explode('/', $alias); }
The crux of the biscuit is that, we created a preprocess function, passed it the variable $var by reference (the preceding &), and defined two variables ($alias and $path_chunk, which are both familiar to us now).
Fantastic, and what does this do? Absolutely nothing since we are not returning anything AND our two variables are tied (scoped) to this function. That being said, let's think back to our two content types, and equally as important, their aliases and how we can use those aliases as a means to an end.
For a recent project (this site), I wanted an <h2> that would display different things depending on the content type. For this example, let's say we want it to print "Blog" when we are viewing a blog page, but when we are viewing a static page we want it to print the actual node title.
function your_theme_name_preprocess(&$vars){ $alias = drupal_get_path_alias($_GET['q']); $path_chunk = explode('/', $alias); switch($path_chunk[0]){ case "blog": $page_head = "Blog"; break; case "static": $page_head = t($vars['title']); //naturally we could use the node objects's title, break; // but for illustration, this works well } $vars['page_head'] = $page_head; }
Note that we wrap the case "static" $page_head var contents with Drupal's t() function, this serves two purposes,
a) it will properly escaping your user input (the URI).
b) the t() function will translate (to the best of it's abilities) your string if your site is set up to handle multiple languages.
Easy as that, we now have the $page_head var available for printing in our theme's templates.
Using a method similar to this for your dynamically changing content can keep your .tpl files clean and it's not to hard to see the other potential uses for this method. Body and div classes also come to mind.

