Override Template Files with Purpose
While core template files are less likely to change during the course of a major release cycle, there are usually massive changes to template files for each major Drupal release, and contributed modules are a constantly moving target. Template files can change at any time, and sometimes drastically. There are many potential reasons for these changes. A module developer may decide to take a different approach, there might be new features or security updates, or there may be no good reason at all. The point is that once you override a template file by adding it to your theme, you are responsible for maintaining it. This can easily get out of hand if you have too many template files.
Another thing to remember is that Drupal is a framework. The whole idea of using Drupal is to take advantage of its modularity. Having too many template files in your theme can essentially remove that modularity; once that happens, your theme can become more of a hassle to maintain than all of Drupal and whatever custom modules you have combined. The key to avoiding this problem is to use overrides sparingly and take advantage of the many tools that Drupal provides.
Just adding the
<div class="inner"> as you did in Listing 16–46 can go a long way in saving you the need to create additional template files. The following tips will help you stay out of trouble when working with templates in Drupal themes:
- Structure for the majority. Explore options for handling one-offs separately by using preprocess functions where possible.
- Take advantage of theme hook suggestions. When the differences between the markup warrant it, use
node--article.tpl.phpto style article nodes and use
theme_links__node()to target only node links.
- Take advantage of CSS classes as arrays. If all you need is a class, don’t create a new template file. For example, block titles are output in a simple
<h2>tag by default. When applying even minimal CSS to
.block h2, you run the risk of affecting
<h2>tags that may end up inside
<div class="content">. Add a class to the title to style against, so you can prevent these issues.