官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 梅河口市| 察隅县| 华安县| 三台县| 七台河市| 景洪市| 长阳| 长治县| 衡阳市| 屏东县| 邯郸县| 张家港市| 都匀市| 盘锦市| 沙湾县| 寻乌县| 筠连县| 彩票| 成都市| 剑阁县| 南陵县| 南乐县| 碌曲县| 西峡县| 西林县| 临湘市| 改则县| 广东省| 肇州县| 通渭县| 马关县| 齐河县| 新龙县| 邵阳县| 来凤县| 南城县| 大洼县| 出国| 旺苍县| 永和县| 五大连池市|