开发者

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开发者_如何学JAVA other template for only certain pages and the rest just default to page.tpl.php?

Thanks! I am on Drupal 6.


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.


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";
  }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜