Drupal 6 Theming Cookbook
上QQ阅读APP看书,第一时间看更新

Understanding the anatomy of a theme

Drupal themes can consist of a multitude of files each with its own purpose, format, and syntax. This recipe will introduce each of these types with an explanation of what they do.

Getting ready

It will be useful to navigate to the Garland folder at themes/garland to browse and view the files inside a typical, fully featured theme. Garland also uses the PHPTemplate theming engine which is the most commonly used and recommended engine across Drupal's core and contributed themes.

How to do it...

The following table outlines the types of files typically found inside a theme's folder and the naming conventions to be followed for some of them.

  

Type           Mandatory?           Description
mytheme.info           Yes           Configuration file which provides information to Drupal about a theme named mytheme.
page.tpl.php           Yes           A template file which determines the layout of all Drupal pages.
node.tpl.php           No           A template file which determines the layout of a node inside a Drupal page.
block.tpl.php           No           A template file which determines the layout of a block.
*.tpl.php           No           Other template files which allow the customization and styling of themable aspects of Drupal.
style.css           No           CSS stylesheet—if this file exists, it will be automatically included in the theme.
script.js           No           Javascript file—if this file exists, it will be automatically included in the theme.
*.js           No           Other Javascript files which will need to be explicitly included in the .info file.
favicon.ico           No           Shortcut icon—if this file exists, it will be automatically included in the theme unless overridden from within Drupal.
logo.png           No           Site logo—if this file exists, it will be automatically included in the theme unless overridden from within Drupal.
screenshot.png           No           Theme preview image—if this file exists, it will be automatically included in the theme.
template.php           No           PHPTemplate master file where some of the more complicated and powerful tweaks and overrides occur.

Perusing the contents of each of the available files will prove very useful as we go along developing our theme.

How it works...

When a theme is added, Drupal first parses its .info file. This file, as its extension suggests, provides information about the theme such as its name, Drupal version compatibility, regions declared, CSS stylesheets used, JavaScript files included, and so on. In other words, Drupal uses it to find out the configuration and features of a theme.

The .info file also specifies the theming engine being used by the theme which, by default, is PHPTemplate. Theme engines allow theme developers to communicate with Drupal using a simpler and more convenient interface commonly via template files. A number of them also introduce their own language formats for use in these template files.

Template files in PHPTemplate themes are those that use the .tpl.php extension. Unlike other engines, these files just use PHP and HTML and do not rely on any special markup languages.

There's more...

Other theme engines besides PHPTemplate are also available. However, only a handful of themes in Drupal's contribution repository rely on them.

Other theme engine types

The PHPTemplate engine is the most widely prevalent theming engine used in the Drupal ecosystem and is a part of the Drupal core package. Themes using other engines such as Smarty or Xtemplate are rare and will be structured quite differently. A list of engines can be found at http://drupal.org/project/Theme+engines.

Tip

Engine-less theme

The Chameleon theme which is a part of core is a theme which does not use a templating engine and relies on straight PHP to get things done.