The previous section, the one describing our CSS, gives a "map" of the HTML that makes a flag link.

This HTML is generated by a template. A template, in Drupal, is a PHP file that is primarily HTML, with tiny bits of PHP sprinkled here and there.

When you install the Flag module, Drupal sees its template file --which is the 'flag.tpl.php' file in the 'theme' folder (inside Flag's own folder)-- and uses it to generate the HTML for the flag links.

It is possible to edit this template file directly, but it is customary to first copy it into your theme folder (e.g., into your Garland folder), and to edit this copy instead.

Drupal 5 and Drupal 6/7 differ a bit in their theming systems:

  1. Drupal 6/7 users: after you copy (or rename, or delete) a template file, you must clear Drupal's cache for it to notice the change. But note that you don't need to clear the cache after you merely edit the content of the file.
  2. Drupal 5 users: The 'theme/README.txt' file contains a function you must copy into your theme's 'template.php' in order to bring to life the templating mechanism.

Template variants

Sometimes you want to theme different flags differently. For example, you may want to theme your "spam" flag as a big red button, the "favorites" flag as a small heart, etc. (These are only examples; it's possible to set a background image using CSS alone, so you shouldn't use templates for this).

Before Drupal looks for 'flag.tpl.php', it looks for 'flag--FLAGNAME.tpl.php'. This lets us create different templates for different flags. FLAGNAME stands for the "machine name" of your flag (with underscores replaced with dashes). Examples:

flag--spam.tpl.php
flag--favorites.tpl.php
flag--things-i-hate.tpl.php