Default theme implementations--Notes

I consider myself lucky to have started with Drupal in a job where I had to do a lot of custom theming. It helped me understand the importance of separation of concerns between the data manipulation and the actual display of the data. It also helped me grok the Presentation-Abstraction-Control stuff that everyone (or mostly Larry Garfield) talks about.

I was hoping that this presentation would be a good resource for my colleagues at DERI, most of whom aren't too familiar with the theme layer. Unfortunately, I'm not sure whether module developers who haven't worked with the theme layer would understand how to make their output themable after watching this presentation... but I picked up some neat tips.

Sidenote: The subtitle of this session was "a guide for module developers who want sweet love from Morten and JohnAlbin"... and you should strive for their sweet love.


The theme layer is about separation of concerns. It separates the business logic from the presentation logic. Without it, the theme would have to understand the output of a module to display it (for example, you would have to copy and paste code snippets into your theme if you installed a new module). 

Each little piece of data is themed separately and then aggregated together into larger pieces, that are then themed together and aggregated into larger pieces.

Theme Hooks

Pass your data through particular theme functions using the theme function:
theme('theme_hook_name', $data)

Use existing hooks

When you are building your own module, first try to use existing theme hooks. Particularly useful ones are: 

  • links
  • image
  • image_style
  • item_list
  • username
  • file_link
  • html_tag
  • more_link
  • pager
  • progress_bar
  • table
http://api.drupal.org/api/group/themeable/7

Using existing hooks means that you don't have to declare your own hook_theme with template/theme function and preprocess hook. It also means that themers don't have to learn special tricks for theming your module. And it also means that you have instant integration with other modules because other modules are aware of these theme hooks and can target the same theme hooks.

Theme Hook Suggestions

theme('hook__suggestion', $vars) allows you to say 'Before using hook, see if there is a hook__suggestion'. This is iterative, so you can have the suggestion 'hook__foo__bar', and it will first look for 'hook_foo_bar', then 'hook_foo', then 'hook'.

Roll Your Own

hook_theme() can be used to register your own theme hooks. You declare the hook and define default variables. Then you set up your default implementation as either a theme function or a template. Theme functions are faster, but templates are easier for themers to get a grasp on and override.

The theme registry goes through all of these implementations and keeps track of what function or template the theme hook points to. This is why you need to clear the theme cache whenever you add a new theme function or template.