Changing Templates
In this chapter you have learned how to create a template for your page and how to customize the page elements. In this section you will see how to change the page templates that are used for different sections of your Web site. You may want to use different templates for each of the following tasks and types of pages:
- Editing content
- Displaying a content type
- User login
- Front page
- Categories
- Offline or maintenance page
Some of these templates are provided by default; others you will need to build from scratch. The online documentation has a complete list of all default templates provided by Drupal at http://drupal.org/node/190815. This section describes several of the page-specific template options.
Custom Front Page
What if you need a front page that has more—or fewer—regions than are provided by a certain template? What if the front page needs to have a bigger banner and a smaller content area? What if you need to make so many changes that it feels like the front page needs a theme all of its own? Fortunately, it is very easy to create a custom front page template for your Drupal site—so easy, in fact, that it is difficult to fill up a whole section of this chapter with information about making a new front page template!
To make a custom front page template, follow these steps:
- Create a new page template file with the name page-front.tpl.php. This is a special file name recognized by Drupal as being a unique template to be used on only the front page of the Web site.
- Clear the theme registry by navigating to Administer, Site configuration, Performance; scroll to the bottom of the Web page and click “clear cached data.”
- Navigate to the front page of your Web site and marvel!
All pages other than the front page will still use the template file page.tpl.php (unless additional page-specific templates are used elsewhere in the site).
Custom Offline Page
Unfortunately, bad things sometimes happen to good Web sites, and the Web sites have to go offline. Drupal provides a default template when a connection cannot be made to the database. In addition, the site can be directed to enter “maintenance” mode so that you can perform some upgrades or other feature enhancements. Figure 4.9 and Figure 4.10 show the default templates for these two offline pages.
Figure 4.9 Offline message for site “under maintenance.”
Figure 4.10 Offline message when the database connection fails.
The offline message template will appear only to visitors who are not authenticated; administrators will still have access to the Web site as they perform their upgrades when a site is “under maintenance.” To customize these pages, complete the following steps:
- Copy the default maintenance page from the Drupal core directory modules/system/maintenance-page.tpl.php to your theme’s directory.
- Make a second copy of the file for the offline template and name it maintenance-page-offline.tpl.php.
You should now have two new files in your theme’s directory:
- maintenance-page.tpl.php: “maintenance” mode
- maintenance-page-offline.tpl.php: “database is offline”
- Adjust these two new templates to suit your needs.
- Open your site’s configuration file in a text editor. (This file is found in sites/yourdomainname.com/settings.php. It is not a theme file, and it is probably write-protected.)
Remove the # symbol from the following lines:
- Line 173: # $conf = array(
- Line 175: # 'theme_default' => 'your_theme_name'
- Line 187: # 'maintenance_theme' => 'your_theme_name'
- Line 214: # );
- Save the changes and make the file read-only again.
The next time you put your Web site into maintenance mode (or if your database server ever goes offline), you will be able to show your customized apology to the world instead of the default Drupal “maintenance” message.
In the section “Custom Front Page,” you learned how to create a custom front page template. The template was activated when the current page was the front page of the Web site. You may take advantage of this technique to target other types of pages as well. Page templates are activated according to Drupal’s internal path for the current page.
Drupal looks through a list of suggested templates from most specific to least specific and checks your theme’s directory for a matching template file. Once it finds a template that matches the criteria, it applies that template to the page. The following list gives examples of the templates that would match for each of the pages:
-
- page-node-5.tpl.php
- page-node.tpl.php
- page.tpl.php
http://www.example.com/node/5/edit
- page-node-edit.tpl.php
- page-node-5.tpl.php
- page-node.tpl.php
- page.tpl.php
http://www.example.com/admin/build/block
- page-admin-build-block.tpl.php
- page-admin-build.tpl.php
- page-admin.tpl.php
- page.tpl.php
http://www.example.com/books/fiction/story-about-ping
- page-node-2665.tpl.php
- page-node.tpl.php
- page.tpl.php
The last item in the list is using a URL alias. There is not a single template that can be used by default to match any of the words in the URL to assign a template. Instead, you must know the exact node ID for the page to find a node-specific template match. These template suggestions exist automatically, so use them whenever you need to create a new template with the same file name and then theme it according to your needs. You may also need to clear the theme registry to see your new template in action.