I've added custom templates for the library site listings and library modules content types on Drupalib. The basic idea is described at http://drupal.org/node/17565, but here are the details of what I've done, in case anyone finds them useful.
Drupalib is running the standard Garland theme, which is a PHPTemplate theme. That means that if I add a .tpl.php file to the Garland* theme directory named after a specific content type, the template in that file will be used to display the teaser and full views of nodes of that type. I added two files, node-content_modules.tpl.php and node-content_site_listing.tpl.php (corresponding to my custom CCK content types) which will override Garland's node.tpl.php file, thereby allowing me to format the HTML output however I want.
node-content_modules.tpl.php looks like this:
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block">
<!-- Teaser view -->
<?php if (!$page): ?>
<h2>Library module: <a href="<?php global $base_url; print $base_url; ?>/node/<?php print $node->nid; ?>"><?php print $node->title ?></h2>
<?php if ($submitted): ?>
<div class="meta">
<span class="submitted"><?php print $submitted ?></span>
</div>
<?php endif; ?>
<div>
<a href="<?php print check_url($node->field_home_page[0]['view']) ?>"><?php print $node->field_home_page[0]['view'] ?></a>
</div>
<div>
<?php print $node->field_description_1[0]['view'] ?>
</div>
<?php endif; ?>
<!-- End teaser view -->
<!-- Full node (i.e., not teaser) view -->
<?php if ($page): ?>
<h2>Library module: <a href="<?php global $base_url; print $base_url; ?>/node/<?php print $node->nid; ?>"><?php print $node->title ?></a></h2>
<?php if ($submitted): ?>
<div class="meta">
<span class="submitted"><?php print $submitted ?></span>
</div>
<?php endif; ?>
<h3>Website</h3>
<div>
<a href="<?php print check_url($node->field_home_page[0]['view']) ?>"><?php print $node->field_home_page[0]['view'] ?></a>
</div>
<h3>Maintainers</h3>
<div>
<?php print $node->field_maintainers[0]['view'] ?>
</div>
<h3>Description</h3>
<div>
<?php print $node->field_description_1[0]['view'] ?>
</div>
<h3>Drupal version compatibility</h3>
<div>
<!-- This CCK field is wrapped in a foreach block because it is configured to allow multiple values -->
<?php foreach ($node->field_requires_drupal_version as $version): ?>
<?php print $version['view'] ?><br />
<?php endforeach; ?>
</div>
<!-- End full node (i.e., not teaser) view -->
<?php endif; ?>
</div>The field names in the library modules CCK content type are field_description_1, field_home_page, field_maintainers, and field_requires_drupal_version. $node->field_requires_drupal_version is wrapped in a foreach block because it is configured to allow multiple values; the other fields hold simple strings so can be printed by using $node->field_xxx[0]['view']. The Content Templates (a.k.a. Contemplate) module provides a web-based alternative to creating templates as files in your theme directory, so you might want to check it out as well. Information on theming CCK fields as opposed to content types, including how to create separate .php.tpl files for individual fields, is available at http://drupal.org/node/206980.
*These files need to go in the same directory as your current theme. This is usually 'themes/garland' (in the case of the Garland theme) but can be 'sites/all/themes/garland' or 'sites/default/themes/garland', depending on how you have set up your site. It would be very convenient if you could put custom templates in a directory under 'sites' while leaving the standard theme files under 'themes', so when it comes time to upgrade Drupal all your custom files are under 'sites.' I tried putting only my custom templates under 'sites/default/themes/garland' and it doesn't work.
$submitted as an alternative to individual elements
It seems that even though the default Garland node.tpl.php file uses
<?php if ($submitted): ?><span class="submitted"><?php print t('!date — !username', array('!username' => theme('username', $node), '!date' => format_date($node->created))); ?></span>
<?php endif; ?>
this much simpler alternative outputs the same HTML:
<?php if ($submitted): ?><div class="meta">
<span class="submitted"><?php print $submitted ?></span>
</div>
<?php endif; ?>
I've updated my templates and the example above to use the simpler version of this template code.
Post new comment