Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use different drupal page templates?

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.

like image 283
matt Avatar asked Dec 17 '22 23:12

matt


2 Answers

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.

like image 73
berkes Avatar answered Jan 11 '23 15:01

berkes


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";
  }
}
like image 33
David Eads Avatar answered Jan 11 '23 15:01

David Eads