- Drupal 6 Theming Cookbook
- Karthik Kumar
- 462字
- 2021-04-13 16:56:44
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.
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.
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:

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.

The non-invasive technique of extending base themes using sub-themes allows for smooth 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.
- PrestaShop 1.3 Theming – Beginner’s Guide
- Moodle 1.9 for Teaching 7/14 Year Olds: Beginner's Guide
- 中文版CorelDRAW 2022基礎教程
- 使用AADL的模型基工程:SAE體系結構分析和設計語言入門
- 鍵盤錄入技術(第2版)
- AutoCAD 2018中文版從入門到精通
- AutoCAD 2018中文版基礎教程
- MATLAB 2022a從入門到精通
- PHP 5 Social Networking
- Oracle Enterprise Manager Grid Control 11g R1: Business Service Management
- Cassandra High Performance Cookbook
- SolidWorks 2017基礎與實例教程
- 中文版3ds Max 2016基礎培訓教程
- 新編中文版Photoshop平面設計入門與提高(第2版)
- 中文版AutoCAD 2014機械設計實戰從入門到精通