Last updated July 11, 2010. Created by WorldFallz on April 27, 2007.
Edited by Shai, traxer, bekasu, getting-wiser. Log in to edit this page.

The list of "template suggestions" for a template can be modified in your template.php file. The following snippet will add page template suggestions for each level of URL alias for the current page. That allows you to, for example, define page-music.tpl.php template which would apply to every page under 'music' path / logical directory. (This information is of particular importance to users of pathauto module who want to create, say, a landing page using a different tpl.php file.)

Drupal 5 version:

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'page':
     
// Add page template suggestions based on the aliased path.
      // For instance, if the current page has an alias of about/history/early,
      // we'll have templates of:
      // page-about-history-early.tpl.php
      // page-about-history.tpl.php
      // page-about.tpl.php
      // Whichever is found first is the one that will be used.
     
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;
      }
      break;
  }
  return
$vars;
}
?>

Note for Zen theme users: If you are creating your own ZEN subtheme under Drupal 5, implement the D6 snippet instead of the D5 version.

Drupal 6 version:

<?php
function phptemplate_preprocess_page(&$vars) {
  if (
module_exists('path')) {
   
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if (
$alias != $_GET['q']) {
     
$template_filename = 'page';
      foreach (
explode('/', $alias) as $path_part) {
       
$template_filename = $template_filename . '-' . $path_part;
       
$vars['template_files'][] = $template_filename;
      }
    }
  }
}
?>

Variations
The following are a couple of variations on this idea.

Change only a CSS class in a page template. Works for making changes in CSS that don't need a different template

<?php
   
if (module_exists('path')) {
       
$alias = drupal_get_path_alias($_GET['q']);
       
$class = explode('/', $alias);
        print
'<div class="page page-'.$class[0].'">';
    }
?>

Limit changes for a specific node type:

<?php
     
// (...)
     
if(arg(0)=='node') {
         
$vars['template_files'][] = 'page_' . $vars['node']->type;
     }
    
// (...)
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

If you use $_GET['q'] you may get a weird page not found message with extra items from the template. Test this with /alias/dkhföadlkfaösklfjöklajf.

Drupal 6 Version:

<?php
function yp_preprocess_node(&$vars, $hook) {
     
$suggestions = array();
     
$vars['profile_status'] = theme('blocks', 'profile_status');
     
$vars['sponsor_right'] = theme('blocks', 'sponsor_right');
     
$vars['kalender_events'] = theme('blocks', 'kalender_events');
      if (
module_exists('path')) {
             
// check for a path alias
           
if (isset($vars['node']->path)) {
             
$alias = ($vars['node']->path);
            }
            else {
             
// otherwise do standard check
             
$alias = drupal_get_path_alias('node/'. $vars['nid']);
            }
          if (
$alias != 'node/'. $vars['nid']) {
             
// Add some special case paths
             
$special_cases = array('press');
             
$path_part = explode('/', $alias);
             
//set special cases variables
             
if (in_array($path_part[0], $special_cases) || $vars['nid'] == variable_get('contact-mail-page-node', 104)) {
               
$vars['submitted'] = '';
              }
             
// template name for current node id and node type
             
$suggestions = array('node-'. $vars['nid']);
             
$suggestions[] = array('node-'. $vars['node']->type);
             
$add_path = '';
              foreach (
explode('/', $alias) as $path_part) {
               
$add_path .= !empty($path_part) ? $path_part : '';
               
//path template name
               
$suggestions[] = 'node-'. $add_path;
              }
           }
         
$vars['template_files'] = isset($vars['template_files']) ? array_merge($vars['template_files'], $suggestions) : $suggestions;
      }
      return
$vars;
}
?>

Here's a useful variant if you're using the subdomain module:

<?php
/* load custom css based on subdomain */
   
if (module_exists('path')) {
       
$alias = drupal_get_path_alias($_GET['q']);
       
$cssdomain = explode('/', $alias);
if (
substr($cssdomain[0], 0, 1) == '~'){
print
'<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/mytheme/style'.$cssdomain[0].'.css?m" />';
}
else{
print
'<link type="text/css" rel="stylesheet" media="all" href="/sites/all/themes/mytheme/style~home.css?m" />';
}
}
?>

Place this snippet in your page.tpl.php file in your active theme's directory, preferably in the header right under the line: "<??php print $styles ?>"
Remember to replace "mytheme" with the name of your theme (or rather, the name of the directory your theme is in).

Then just make a bunch of css files, one for each subdomain, and place them in your theme's directory. Follow this naming pattern:
style~blog.css == for your blog.mysite.com subdomain
style~home.css == for your main domain (no subdomain)

Hi, not sure if this is the right place to ask this but wasn't sure where else to do so...

I have an ecommerce site w/SSL set up. The problem is that I'm also using SiteMeter for traffic stats, and the SiteMeter code includes a couple of non-secure (HTTP://) embedded file links, which causes browsers to give a "partially secure" warning message. The way SiteMeter code works, it has to be entered directly into the page.tpl.php file, immediately before the tag.

As far as I can tell, I can either a) abandon SiteMeter for this site (and any others that require SSL) or b) find a way of creating a custom page.tpl.php file, without the SiteMeter code, that *only* loads for HTTPS:// pages.

Any thoughts would be greatly appreciated.

The comment title says is all …

If it helps anyone reading this page:

Using the SwitchTheme module - http://drupal.org/project/switchtheme ...

You can load a URL like this:
mywebsite.com/node/16/?theme=apples
mywebsite.com/node/23/?theme=oranges

Works for me on Drupal 7. I'm displaying blog posts in an iFrame on a different website and use a special theme for it.

Theme template can be overridden with URL alias in the following way.

1. Preprocessor function in theme template file
2. Get the URL alias and choose the template file.

Explanation with code is given in the below link,
http://mydons.com/how-to-create-a-template-for-page-created-in-drupal-ad...