Using template (.tpl.php) files in your own module

Normally (.tpl.php) template files reside in the theme folder.

But sometimes custom modules may need to override the default template files -- page, node, etc. -- provided by Drupal. It can be tricky figuring out how to do this, so this page will present approaches for Drupal 5, 6, and 7.

Why would you want to override a template file in a module rather than in the theme?
There are a number of reasons a module would need to have control of how a piece of content is presented. A module may provide a new content type with a custom node template. In the case of the Print module, it is necessary to exclude the header, footer, and sidebars and then add some other elements.

The Drupal 6 and 7 approaches are similar: telling the theme system to look in the module directory for templates as well as the theme directory. In both cases it may be necessary to limit when the custom template is used. I.e., if a module has its own page.tpl.php, it probably should not be used for every page on the site.

This can be done by naming the custom template in such a way that it will only be used in certain cases. (See Drupal 6 template suggestions and Drupal 7 template suggestions.)

For finer control it can also be done using a preprocess function.

Overview of theme files

A theme is a collection of files that define the presentation layer. You can also create one or more "sub-themes" or variations on a theme. Only the .info file is required, but most themes and sub-themes will use other files as well. The following diagram illustrates the files that are found in a typical theme and sub-theme.

Drupal 6

theming requirements

Drupal 7

drupal 7 theme templates

Subscribe with RSS Subscribe to RSS - .tpl.php