Using Multiple Templates

This is the third article in the series of Packpub Drupal theming articles from their new book Drupal 6 Themes—**Using Multiple Templates in Drupal 6**.

Most advanced sites built today employ multiple page templates. In this section, we will look at the most common scenarios and how to address them with a PHPTemplate theme.

> While there are many good reasons for running multiple page templates, you should not create additional templates solely for the purpose of disabling regions to hide blocks. While the approach will work, it will result in a performance hit for the site, as the system will still produce the blocks, only to then wind up not displaying them for the pages. The better practice is to control your block visibility.

## Using a Separate Admin Theme

With the arrival of Drupal 5, one of the most common Drupal user requests was satisfied; that is, the ability to easily designate a separate admin theme. In Drupal, designating a separate theme for your admin interface remains a simple matter that you can handle directly from within the admin system.

To designate a separate theme for your admin section, follow these steps:

1. Log in and access your site's admin system.
2. Go to **Administer | Site configuration | Administration theme**.
3. Select the theme you desire from the drop-down box listing all the installed themes.
4. Click **Save configuration**, and your selected theme should appear immediately.

## Multiple Page or Section Templates

In contrast to the complete ease of setting up a separate administration theme is the comparative difficulty of setting up multiple templates for different pages or sections.

The bad news is that there is no admin system shortcut—you must manually create the various templates and customize them to suit your needs. The good news is that creating and implementing additional templates is not difficult and it is possible to attain a high degree of granularity with the techniques described below. Indeed, should you be so inclined, you could literally define a distinct template for each individual page of your site.

Drupal employs an order of precedence based on a naming convention (or "suggestions" as they are now being called on the Drupal site). You can unlock the granularity of the system through proper application of the naming convention. It is possible, for example, to associate templates with every element on the path, or with specific users, or with a particular functionality—all through the simple process of creating a new template and naming it appropriately.

The system will search for alternative templates, preferring the specific to the general, and failing to find a more specific template, will apply the default *page.tpl.php*. Consider the following example of the order of precedence and the naming convention in action.

![Drupal template suggestions][Drupal template suggestions]

The custom templates above could be used to override the default page.tpl.php and theme either an entire node (page-node.tpl.php), or simply the node with an ID of 1 (page-node-1.tpl.php),or the node in edit mode (page-node-edit.tpl.php), depending on the name given the template.

> In the example above, the page-node templates would be applied to the node in full page view. In contrast, should you wish to theme the node in its entirety, you would need to intercept and override the default node.tpl.php.

The fundamental methodology of the system is to use the first template file it finds and ignore other, more general templates (if any). This basic principle, combined with proper naming of the templates, gives you control over the template used in various situations.

> The default suggestions provided by the Drupal system should be sufficient for the vast majority of theme developers. However, if you find that you need additional suggestions beyond those provided by the system, it is possible to extend your site and add new suggestions. See []( for a discussion of this advanced Drupal theming technique.

Let's take a series of four examples to show how this feature can be used to provide solutions to common problems:

1. Create a unique homepage template.
2. Use a different template for a group of pages.
3. Assign a specific template to a specific page.
4. Designate a specific template for a specific user.

## Create a Unique Homepage Template

Let's assume that you wish to set up a unique template for the homepage of a site. Employing separate templates for the homepage and the interior pages is one of the most common requests web developers hear.

With Drupal, you can, without having to create a new template, achieve some variety within a theme by controlling the visibility of blocks on the homepage. If that simple technique does not give you enough flexibility, you will need to consider using a dedicated template that is purpose-built for your homepage content.

The easiest way to set up a distinct front page template is to copy the existing page.tpl.php file, rename it, and make your changes to the new file. Alternatively, you can create a new file from scratch. In either situation, your front-page-specific template must be named page-front.tpl.php. The system will automatically display your new file for the site's homepage, and use the default page.tpl.php for the rest of the site.

> Note that page-front.tpl.php is whatever page you specify as the site's front page via the site configuration settings. To override the default homepage setting visit Administer | Site configuration | Site information, then enter the URL you desire into the field labeled Default home page.

## Use a Different Template for a Group of Pages

Next, let's associate a template with a group of pages. You can provide a template to be used by any distinct group of pages, using as your guide the path for the pages. For example, to theme all the user pages you would create the template page-user.tpl.php.

To theme according to the type of content, you can associate your page template with a specific node, for example, all blog entry pages can be controlled by the filepage-blog-tpl.php. The table below presents a list of suggestions you can employ to theme various pages associated with the default functionalities in the Drupal system.

PHPTemplate Suggestions
Suggestion Affected Page
page-user.tpl.php User pages.
page-blog.tpl.php Blog pages (but not the individual node pages).
page-forum.tpl.php Forum pages (but not the individual node pages).
page-book.tpl.php Book pages (but not the individual node pages).
page-contact.tpl.php Contact form (but not the form content).

## Assign a Specific Template to a Specific Page

Taking this to its extreme, you can associate a specific template with a specific page. By way of example, assume we wish to provide a unique template for a specific content item. Let's assume our example page is located at The path of this specific page gives you a number of options. We could theme this page with any of the following templates (in addition to the default page.tpl.php):

* page-node.tpl.php
* page-node-2.tpl.php
* page-node-edit.tpl.php

**A Note on Templates and URLs**
Drupal bases the template order of precedence on the default path generated by the system. If the site is using a module like pathauto, which alters the path that appears to site visitors, remember that your templates will still be displayed based on the original paths. The exception here being page-front.tpl.php, which will be applied to whatever page you specify as the site's front page via the site configuration settings (**Administer | Site configuration| Site information**).

## Designate a Specific Template for a Specific User

Assume that you want to add a personalized theme for the user with the ID of 1(the Drupal equivalent of a Super Administrator). To do this, copy the existing page.tpl.php file, rename it to reflect its association with the specific user, and make any changes to the new file. To associate the new template file with the user, name the file: page-user-1.tpl.

Now, when user 1 logs into the site, they will be presented with this template. Only user 1 will see this template and only when he or she is logged in and visiting the account page.

> The official Drupal site includes a collection of snippets relating to the creation of custom templates for user profile pages. The discussion is instructive and worth review, though you should always be a bit cautious with user-submitted code snippets as they are not official releases from the Drupal Association. See, [](

Drupal 6 Themes

Create new themes for your Drupal 6 site with clean layout and powerful CSS styling

  • Learn to create new Drupal 6 themes
  • No experience of Drupal theming required
  • Techniques and tools for creating and modifying themes
  • A complete guide to the system's themable elements

[Drupal template suggestions]: /files/drupal6-article-part1-image1.png

Last updated 14th April, 2009 - 10:57pm

Authored by Jeff Burnz on