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:
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. :-)
Comments
I generally agree with you;
I generally agree with you; I prefer not to use contemplate, or not use it too much. However, there are 2 things I really find it useful for:
1) Resetting a node's output to just the basics. This is less of an issue now that CCK can be more controlled, but at the time I wanted to get rid of CCK's extra output and preserve things like upload.module's output. contemplate let me do that.
2) It gives a nice view into the node so you can see what variables you need.
Thanks for this post. I've
Thanks for this post. I've been kicking myself, thinking I was too stupid to figure out why my images weren't showing up.
I think you hit the answer. I was using CCK and contemplate fine, but as soon as I added imagecache things started to go haywire and I couldn't figure out why.
I guess I need to dump the contemplate module, as that seems to be the weak link, at least in this combination. Otherwise, I have to say that I really like contemplate because it makes things much easier to get where I want to go without much trial and error. But then, that's largely because I'm not really a PHP coder.
#2 is nicely serviced by
#2 is nicely serviced by devel.module's 'devel render' tab. check it out on any node view.
Good points Barry. As a
Good points Barry. As a not-quite-expert-themer-just-yet, I've found contemplate useful for easily theming cck teasers and full nodes separately. While I understand how to use a tpl file to theme a full node, how do I theme a teaser using a template file?
Thanks :)
To theme a teaser in a
To theme a teaser in a template file, use the $teaser variable. For example, in node-typename.tpl.php:
<?php /* beginning of the template */<div class="node">
<?php if ($teaser): ?>
... teaser goes here ...
<?php else: ?>
... non-teaser goes here ...
<?php endif; ?>
</div><!--node-->
?>
I personally am moving away from custom node template files as much as possible. Instead, I'm using CCK's display fields options and the viewfield module or Drupal 5's $node->content array. I'll be talking about the former during my seminar (http://druplinars.com/site-building-with-drupal). The latter requires coding in a custom module; that's a subject for a different day.
I think contemplate has a
I think contemplate has a very good point: it is by far less intimidating than node.tpl files, wich I think is important, as people can start theming different node types without even being themers.
In the long run, they will realize what are contemplate faults and will jump to other theming tactics, but contemplate will have the merit to introduce them to the intimidating world of theming
Post new comment