Core templates and suggestions

Last modified: September 30, 2009 - 20:19

Note: There is a bug which prevents derivative template files from being detected if the base template file is not also present. See #279573: Themes can't use node-story.tpl.php without node.tpl.php for more info.

These templates and suggestions are also available in a cheat sheet (PDF)

Default templates:

These are the default template (.tpl.php) files provided by core. Documentation on the variables and purpose of these templates are located inside the templates. There is a default set of variables available to all templates.

The templates handled by PHPTemplate in 5.x were also moved. PHPTemplate no longer handles template files.

In order to override these templates, all you need to do is copy them into your theme folder and clear the theme registry.

Aggregator

"modules/aggregator/..."

Block

"modules/system/..."

"modules/block/..."

Book

"modules/book/..."

Comment

"modules/comment/..."

Forum

"modules/forum/..."

Node

"modules/node/..."

Poll

"modules/poll/..."

Profile

"modules/profile/..."

Search

"modules/search/..."

User

"modules/user/..."

System

"modules/system/..."

Template suggestions:

What's a template suggestion? It's an alternate template (tpl) file that you have created to override the base or original tpl file. Suggestions only work when they are placed in the same directory as the base templates. In other words, if you want comment-blog.tpl.php to display in your browser, comment.tpl.php also needs to exist inside your theme and the two files must be in the same directory.

Custom suggestions beyond the ones listed below can be created. See the page Working with template suggestions.

block-[region|[module|-delta]].tpl.php

base template: block.tpl.php

Suggestions made based on these factors in this order:

  1. block-module-delta.tpl.php
  2. block-module.tpl.php
  3. block-region.tpl.php

"module" being the name of the module and "delta", the internal id assigned to the block by the module. For example, "block-user-1.tpl.php" would be used for the default user navigation block since it was created by the user module with the id of 1. "region" will take effect for specific regions.

comment-[type].tpl.php

base template: comment.tpl.php

Support was added to create comment-type.tpl.php files, to format comments of a certain node type differently than other comments in the site. Similar to node-[type].tpl.php but for comments.

comment-wrapper-[type].tpl.php

base template: comment-wrapper.tpl.php

Similar to the above but for the wrapper template.

forums-[[container|topic]-forumID].tpl.php

base template: forums.tpl.php

Template suggestions added based in these factors, in this order.

For forum containers:

  1. forums-containers-forumID.tpl.php
  2. forums-forumID.tpl.php
  3. forums-containers.tpl.php

For forum topics:

  1. forums-topics-forumID.tpl.php
  2. forums-forumID.tpl.php
  3. forums-topics.tpl.php
maintenance-page-[offline].tpl.php

base template: maintenance-page.tpl.php

This applies when the database fails. Useful for presenting a friendlier page without error messages. Theming the maintenance page must properly setup first.

node-[type].tpl.php

base template: node.tpl.php

Node content type, e.g., "node-story.tpl.php", "node-blog.tpl.php", etc. Note that in order to override the template for a specific node type, the theme must also implement the base node.tpl.php file. If this file is omitted, the theme will not even bother looking for node-[type].tpl.php files.

page-[front|internal/path].tpl.php

base template: page.tpl.php

The suggestions are numerous. The one that takes precedence is for the front page. The rest are based on the internal path of the current page. Do not confuse the internal path to path aliases which are not accounted for. Keep in mind that the commonly used Path auto module works its magic through path aliases.

The front page can be set through "Administrator > Site configuration > Site information". Anything set there will trigger the suggestion of "page-front.tpl.php" for it.

The list of suggested template files in order of specificity based on internal paths. One suggestion is made for every element of the current path, though numeric elements are not carried to subsequent suggestions. For example, "http://www.example.com/node/1/edit" would result in the following suggestions:

  1. page-node-edit.tpl.php
  2. page-node-1.tpl.php
  3. page-node.tpl.php
  4. page.tpl.php
poll-results-[block].tpl.php

base template: poll-results.tpl.php

The theme function that generates poll results are shared for nodes and blocks. The default is to use it for nodes but a suggestion is made for rendering them inside block regions. This suggestion is used by default and the template file is located inside "modules/poll/poll-results-block.tpl.php".

poll-vote-[block].tpl.php

base template: poll-vote.tpl.php

Similar to poll-results-[block].tpl.php but for generating the voting form. You must provide your own template to for it to take effect.

poll-bar-[block].tpl.php

base template: poll-bar.tpl.php

Same as poll-vote-[block].tpl.php but for generating individual bars.

profile-wrapper-[field].tpl.php

base template: profile-wrapper.tpl.php

The profile wrapper template is used when browsing the member listings page. When browsing specific fields, a suggestion is made with the field name. For example, http://drupal.org/profile/country/Belgium would "suggest profile-wrapper-country.tpl.php".

search-results-[searchType].tpl.php

base template: search-results.tpl.php

search-results.tpl.php is the default wrapper for search results. Depending on type of search different suggestions are made. For example, "example.com/search/node/Search+Term" would result in "search-results-node.tpl.php" being used. Compare that with "example.com/search/user/bob" resulting in "search-results-user.tpl.php". Modules can extend search types adding more suggestions of their type.

search-result-[searchType].tpl.php

base template: search-result.tpl.php

The same as above but for individual search results.

Automatic node-[nid].tpl.php template suggestions

psynaptic - November 18, 2008 - 13:45

Using <?php $vars['template_file'] ?> it is possible to create your own template file suggestions. Here is an example to allow use of node-[nid].tpl.php files.

<?php
/**
* Override or insert variables into the node templates.
*
* @param $vars
*   An array of variables to pass to the theme template.
* @param $hook
*   The name of the template being rendered ("node" in this case.)
*/
function phptemplate_preprocess_node(&$vars, $hook) {
 
$node = $vars['node'];
 
$vars['template_file'] = 'node-'. $node->nid;
}
?>

Drupal Contributions API

Teasers

Karlheinz - July 13, 2009 - 14:41

One thing that I had to find out by trial and error:

A node's teaser view is generated by node.tpl.php. If you want to show or hide node contents for teaser views, you would check to see if the $teaser variable is true.

An example: Say you wanted to have taxonomy terms visible in a node's full view, but not in the teaser. In node.tpl.php, you would change this:

<?php if ($terms): ?>
  <div class="terms terms-inline"><?php echo $terms ?></div>
<?php endif;?>

...to this:

<?php if ($terms && !$teaser): ?>
  <div class="terms terms-inline"><?php echo $terms ?></div>
<?php endif;?>

Note the && !$teaser part of the PHP if statement.

It appears that the $teaser variable is only available to node.tpl.php, but I could be wrong about this.

-Karlheinz

Custom node on front page (node-front.tpl.php)

NoCoolNamesRemain - October 28, 2009 - 17:25

Edit /sites/all/themes/YOURTHEME/template.php to include:

<?php

function phptemplate_preprocess_node(&$vars) {
 
//default template suggestions for all nodes
 
$vars['template_files'] = array();
 
$vars['template_files'][] = 'node';

  if (
drupal_is_front_page()) {
   
$vars['template_files'][] = 'node-front';
  }

 
//individual node being displayed
 
if($vars['page']) {
   
$vars['template_files'][] = 'node-page';
   
$vars['template_files'][] = 'node-'.$vars['node']->type.'-page';
   
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-page';
  }
 
//multiple nodes being displayed on one page in either teaser
  //or full view
 
else {
   
//template suggestions for nodes in general
   
$vars['template_files'][] = 'node-'.$vars['node']->type;
   
$vars['template_files'][] = 'node-'.$vars['node']->nid;

   
//template suggestions for nodes in teaser view
    //more granular control
   
if($vars['teaser']) {
     
$vars['template_files'][] = 'node-'.$vars['node']->type.'-teaser';
     
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-teaser';
    }
  }

 
//echo "<pre>" . print_r($vars['template_files'], TRUE) . "</pre>"; //TESTING
}
?>

Then copy /modules/node/node.tpl.php to /sites/all/themes/YOURTHEME/node-front.tpl.php and customize it to your needs.

see also Extending Drupal Templating

 
 

Drupal is a registered trademark of Dries Buytaert.