views:

47

answers:

3

Hello,

I have two page template files, one that is just 'page.tpl.php' and another for a subset of pages.

How can I apply this other template for only certain pages and the rest just default to page.tpl.php?

Thanks! I am on Drupal 6.

+2  A: 

In Drupal the templates follow so called "suggestions".

For pages, suggestions are built up using the url-path.

On page /foo/bar you can name your page.tpl.php as follows:

  • page.tpl.php
  • page-foo.tpl.php
  • page-foo-bar.tpl.php

Drupal will look from most specific to least specific and pick the first one found. If page-foo-bar.tpl.php is found, it will use that for foo/bar if not found it will look on. If then it finds page-foo, it will use that.

page-foo.tpl.php will also be used for /foo/beserk foo/pirate and foo/parrot, unless off course, you create a page-foo-parrot.tpl.php.

The front-page has no path (obviously) but a suggestion for the fron-page does exist: page-front.tpl.php will be used only for the frontpage.

berkes
If you want to find out template suggestions for any piece of content, use http://drupal.org/project/devel_themer, wich adds a kind of firebug console that allows you to select any item on your page and will display lots of theme information for that. including all suggestions.
berkes
A: 

You may also want to check out the Sections module, it allows selecting a separate theme for a set of pages. The selection of pages is the same as on the block display configuration page - Show on every page except the listed pages, Show on only the listed pages, Show if the following PHP code returns TRUE.

I used it on one of the sites I created, and it works great.

NPC
I like that module :P
berkes
Though if you need to change your page.tpl.php depending on a page, then you need to use the other suggestions. Sorry if I don't help.
NPC
+4  A: 

In addition to Drupal's default suggestion system, you can implement template_preprocess_page in your theme and change the template suggestion array:

function my_theme_preprocess_page(&$vars) {
  if ($vars['some_var'] == 'some_value') {
    $vars['template_files'][] = "page-special";
  }
}

See Working with template suggestions.

You can also force the template file rather than adding a suggestion:

function my_theme_preprocess_page(&$vars) {
  if ($vars['some_var'] == 'some_value') {
    $vars['template_file'] = "page-special";
  }
}
David Eads