Drupal 6 Theming Cookbook
上QQ阅读APP看书,第一时间看更新

Overriding base theme elements in a sub-theme

This recipe details the steps involved in overriding a base theme template file. We will be restructuring the layout of a Drupal node by modifying the node.tpl.php template.

Getting ready

We will be using the mytheme sub-theme which was created in the previous recipe.

How to do it...

As we are dealing with a sub-theme here, it is by default relying on the template files of its base theme. To override the base file used to theme the layout of a node, copy the node.tpl.php file from the base theme's folder—themes/garland—to the sites/themes/mytheme folder. Opening the new file in an editor should bring up something similar to the following:

<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<p><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></p>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
<div class="content clear-block">
<?php print $content ?>
</div>
<div class="clear-block">
<div class="meta"> <?php if ($taxonomy): ?> <div class="terms"><?php print $terms ?></div> <?php endif;?> </div>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>

The highlighted lines in the preceding code excerpt indicate the code we are looking to modify. To elaborate, we are going to move the taxonomy terms DIV from the bottom to a position further above, right next to the submitted DIV. Doing so will result in the node.tpl.php file now looking like this:

<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<?php if ($page == 0): ?>
<p><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></p>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>
<?php if ($taxonomy): ?> <div class="terms"><?php print $terms ?></div> <?php endif;?>
<div class="content clear-block">
<?php print $content ?>
</div>
<div class="clear-block">
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>

Once this has been done, save the file and exit the editor. As we have made changes to the template system, we will need to rebuild the theme registry, or as is recommended throughout this book, simply clear the entire Drupal cache.

How it works...

For performance purposes, Drupal maintains a registry of all the stylesheets which have been included, the template files which are available, the theme functions which have been declared, and so on. As our theme initially had no node.tpl.php file in the mytheme folder, Drupal fell back to the node.tpl.php file of the base theme which, in this case, is Garland. However, once we added one to the mytheme folder, we needed to rebuild this registry so that Drupal became aware of our changes. Once this was done, Drupal used the updated node.tpl.php file the next time a node was displayed.

The following screenshots provide a before and after comparison of an example node:

How it works...

In the following screenshot, we can see our modified template file in action as the position of the taxonomy term Category 1 has moved from the bottom to the top of the node.

How it works...

There's more...

The non-invasive technique of extending base themes using sub-themes allows for smooth upgrades.

Clean upgrades

If we had modified the node.tpl.php file inside Garland, the next time our Drupal installation is upgraded, we would have very likely forgotten about our changes and overwritten them during the upgrade process. By using a sub-theme, we can now upgrade Drupal without any fear of losing any changes we have made.

Another positive is that if bugs have been fixed in Garland, they will seamlessly propagate downriver to our sub-theme.