Anatomy of a Drupal 6 theme

Last modified: August 14, 2009 - 23:36

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.

theming requirements

.info (required)

All that is required for Drupal to see your theme is a ".info" file. Meta data, style sheets, JavaScripts, block regions and more can be defined here. Everything else is optional.

The internal name of the theme is also derived from this file. For example, if it is named "drop.info", then Drupal will see the name of the theme as "drop". Drupal 5 and below used the name of the enclosing folder of the theme.

Info files for themes are new in Drupal 6. In version 5, .info files were used solely for modules.

template files (.tpl.php)

These templates are used for the xHTML markup and PHP variables. In some situations they may output other types of data --xml rss for example. Each .tpl.php file handles the output of a specific themable chunk of data, and in some situations it can handle multiple .tpl.php files through suggestions. They are optional, and if none exists in your theme it will fall back to the default output. Refrain from having complex logic in these files. In most cases, it should be straight xHTML tags and PHP variables. A handful of these templates exist in directories where core and contributed modules exist. Copying them to your theme folder will force Drupal to read your version.

Note: The theme registry caches information about the available theming data. You must reset it when adding or removing template files or theme functions from your theme.

template.php

For all the conditional logic and data processing of the output, there is the template.php file. It is not required, but to keep the .tpl.php files tidy it can be used to hold preprocessors for generating variables before they are merged with the markup inside .tpl.php files. Custom functions, overriding theme functions or any other customization of the raw output should also be done here. This file must start with a PHP opening tag "<?php", but the close tag is not needed and it is recommended that you omit it.

Sub-themes

On the surface, sub-themes behave just like any other theme. The only differences is that they inherit the resources from their parent themes. To create one, a "base theme" entry inside the .info file is needed. From there it will inherit the resources from its parent theme. There can be multiple levels of inheritance; i.e., a sub-theme can declare another sub-theme as its base. There are no hard set limits to this.

Drupal 5 and below required sub-themes to be in sub-directories of the parent theme. This is no longer the case.

Others
  • The logo and screen shot is not absolutely necessary for the theme to function, but it is recommended, especially if you are contributing your theme to the Drupal repository. Screenshots will show inside the theme administration page and the user account settings for selecting themes when the appropriate permissions are set. See the screenshot guidelines for more information.
  • To supply administrative UI settings or "features" beyond logo, search, mission, etc., a "theme-settings.php" file can be used. This is an advanced feature. More information can be found in the Advanced settings handbook page.
  • For color module support, a "color" directory with a "color.inc" file is needed along with various support files.
  • If you want to base your work on a core theme, use sub-theming or make a copy and rename the theme. Directly modifying Garland or Minnelli is strongly discouraged, since they are used for the install and upgrade process.
  • All non-Core or modifications to Core themes should be installed under the "sites/all/themes" directory to keep them separate from core files. If you plan to run multiple sites from a single Drupal code base, you can make a theme available to a specific site rather than all sites; read about how to set this up in Multi-site installations.

theming basics

tizone - August 22, 2009 - 22:34

I am new to Drupal, and I think the Getting Started guide is really good. However, I feel that Theming guide is too technical for newies. I am writing at this time, because I will probably not feel that in a few days, when I have read something more about theming.

It is worth making it clear that probably I am not learning Drupal in the way most people do. I think that I have understood the basics concepts, and now I want to be able to create a new theme for my site. So, it would be good (at least for me), to have a Theming guide that starts from the really basic concepts, explaining regions, "themable chunks of data" and examples about it, preprocessors, etc. Of course, maybe this guide is not thought to be that kind of guide, and maybe there is not general interest on that kind of guide.

Thanks very much.

QUICKSTART GUIDE

SimonEast - August 26, 2009 - 03:53

OK, well in the spirit of community participation, I've found some helpful links for newbies:

Basic Drupal Theme Tutorial
http://tips.webdesign10.com/how-to-make-a-drupal-theme

From Photoshop to Drupal Theme in 3 Steps
http://www.xalking.com/blog/psd-drupal-theme-tutorial-part-i
http://www.xalking.com/blog/psd-drupal-theme-tutorial-part-ii
http://www.xalking.com/blog/psd-drupal-theme-tutorial-part-iii
(plus some other great Drupal articles from same author)

Video Tutorial on Modifying Existing Themes
http://gotdrupal.com/videos/drupal-theme-makeover

Changing the dynamic HTML coming out of Drupal (what they call "Overriding themable output")
http://drupalsn.com/learn-drupal/drupal-tutorials/drupal-theme-function-...

Feel free to post any other good links, or reply with a thanks if these helped you.

In the paragraph explaining

charlie1234 - November 7, 2009 - 06:35

In the paragraph explaining template.php above, it states:

This file must start with a PHP opening tag but the close tag is not needed and it is recommended that you omit it.

Why? All of the template.php files I've seen have a closing ?> php tag. I also find it odd that something as specific as this be recommended but not forced. Thanks for any info to back up this statement.

 
 

Drupal is a registered trademark of Dries Buytaert.