CCK, imagefield, imagecache, and contemplate snafu

I'm not a huge fan of the Drupal "contemplate" module (see below), but Jeff Robbins recently recommended it to me so I decided to give it a try. I'm using it with a CCK type that has an imagefield field and I'm using imagecache to control the appearance of the image. I have a separate imagecache preset for the body and teaser view and use the imagefield settings that allow me to select which preset I want to use in each view. I think this setup is fairly typical but it turns out not to work.

The problem seems to be that the contemplate module assumes that, while a node type will have a different template for the body and teaser view that contains different fields, each field within the node will look the same no matter which view it is displayed in. This assumption fails for imagefields fields because the imagecache preset used to format the <img> tag can be different. The precise details of the code are not that important but the upshot is that contemplate will always display imagefield images formatted with the teaser imagecache preset, never the body imagecache preset.

Suppose you have a CCK type with an imagefield field called screenshots. The default contemplate template will contain:

  <div class="field-items">
    <?php foreach ((array)$field_screenshots as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>

This needs to be replaced. First, add the following function to your template.php file:

function theme_imagecache_field($namespace, $field, $attrs = NULL) {
  return theme_imagecache($namespace, $field['filepath'], $field['alt'],
  $field['title'], $attrs);
};

Then, change the teaser template to look like this (note that only the innermost PHP print statement has changed):

  <div class="field-items">
    <?php foreach ((array)$field_screenshots as $item) { ?>
      <div class="field-item"><?php print theme('imagecache_field', 'screenshot_teaser', $item); ?></div>
    <?php } ?>
  </div>

This assumes that your imagecache preset name is screenshot_teaser. You must also make the same change to the body template but, of course, provide the appropriate imagecache preset name. If contemplate had access to the CCK imagefield field object we could get the selected preset names from it instead of having to hardcode them, but the field object is not available.

As promised, here are the reasons that I do not like the contemplate module:

  • It's very purpose is to put node templates in the database instead of source files. Source files can be put under revision control, however, and the database can't. Thus, contemplate makes maintaining node templates more difficult.
  • If you make a syntax error in PHP code in a contemplate template, it is impossible to fix without modifying the database directly because trying to view the template edit page triggers the syntax error and kills Drupal.
  • An HTML textarea is much less functional than practically any other code editor available.

I realize that contemplate can be useful for simple templates or users who are not familiar/comfortable with using text editors and synchronizing files to their web server. No one is forcing me to use contemplate, of course, and I still have tremendous respect for Jeff Robbins's work. :-)