Last updated January 26, 2012. Created by Carolyn on March 11, 2011.
Edited by nevets, WorldFallz, Setca, tim.plunkett. Log in to edit this page.
- Template suggestions:
-
What's a template suggestion? It's an alternate template (.tpl.php) file that you have created to override the base or original template file. Suggestions only work when they are placed in the same directory as the base templates.
Custom Template Suggestions
Custom suggestions beyond the ones listed below can be created. See the page Working with template suggestions.
Default Template Suggestions in Core
- block--[region|[module|--delta]].tpl.php
-
base template: block.tpl.php
Template suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:
- block--module--delta.tpl.php
- block--module.tpl.php
- 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--block--1.tpl.php" would be used for the first user-submitted block added from the block administration screen since it was created by the block module with the id of 1. "region" will take effect for specific regions. An example of a region-specific template would be block--sidebar_first.tpl.php. If you had a block created by a custom module called "custom" and a delta of "my-block", the template suggestion would be called "block--custom--my-block.tpl.php."
- comment--[node-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. For example, a comment made on an article-type node would be "comment--node-article.tpl.php".
- comment-wrapper--[node-type].tpl.php
-
base template: comment-wrapper.tpl.php
Similar to the above but for the wrapper template.
- field--[type|name[--content-type]|content-type].tpl.php
-
base template: field.tpl.php
Template suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:
- field--field-name--content-type.tpl.php
- field--content-type.tpl.php
- field--field-name.tpl.php
- field--field-type.tpl.php
Remember to include "field-" in custom field names, e.g: field--field-phone.tpl.php
- forums--[[container|topic]--forumID].tpl.php
-
base template: forums.tpl.php
Template suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:
For forum containers:
- forums--containers--forumID.tpl.php
- forums--forumID.tpl.php
- forums--containers.tpl.php
For forum topics:
- forums--topics--forumID.tpl.php
- forums--forumID.tpl.php
- forums--topics.tpl.php
- html.tpl.php
-
base template: html.tpl.php
See html.tpl.php in the Drupal API documentation for more information.
- 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 be properly setup first.
- node--[type|nodeid].tpl.php
-
base template: node.tpl.php
Template suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:
- node--nodeid.tpl.php
- node--type.tpl.php
- node.tpl.php
See node.tpl.php in the Drupal API documentation for more information.
- 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 at "Administration > Configuration > System > Site information." In Drupal 6, at "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 is 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:
- page--node--edit.tpl.php
- page--node--1.tpl.php
- page--node.tpl.php
- page.tpl.php
Also see page.tpl.php in the Drupal API documentation for more information.
How Drupal determines page template suggestions based on path
Here is another explanation based on the theme_get_suggestions() function:
The list of possible templates for a given page is generated by Drupal through the theme_get_suggestions() function, which is called by the template_preprocess_page() function.
The Drupal path of the page is first broken up into its components. As mentioned above, the Drupal path is not any of its aliases: there is one and only one Drupal path for a page. For the examples "http://www.example.com/node/1/edit" and "http://www.example.com/mysitename?q=node/1/edit", the Drupal path is node/1/edit, and its components are "node", 1, and "edit".
Next, the prefix is set to "page". Then, for every component, the following logic is followed:
- If the component is a number, add the prefix plus "__%" to the list of suggestions.
- Regardless of whether the component is a number or not, add the prefix plus "__" plus the component to the list of suggestions.
- If the component is not a number, append "__" plus the component to the prefix.
After the list of components is iterated through, if the page is the front page (as set through "Administration > Configuration > System > Site information."), then "page__front" is added to the list of suggestions.
Note that eventually, to turn a suggestion into an actual file name, "__" gets turned into "--", and ".tpl.php" gets appended to the suggestion. Thus, for node/1/edit, we get the following list of suggestions:
- page.tpl.php (this is always a suggestion)
- page--node.tpl.php (and prefix is set to page__node)
- page--node--%.tpl.php
- page--node--1.tpl.php (prefix is not changed because the component is a number)
- page--node--edit.tpl.php (and prefix is set to page__node__edit)
- page--front.tpl.php (but only if node/1/edit is the front page)
When the page is actually rendered, the last suggestion is checked. If it exists, that suggestion is used. Otherwise the next suggestion up is checked, and so on. Of course, if none of the overriding suggestions exist, page.tpl.php is the final suggestion. This also explains why page--front.tpl.php, if it exists, overrides any other suggestion for the front page: it is always the last suggestion for the page designated as the front page.
- 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 at "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 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".
- region--[region].tpl.php
-
base template: region.tpl.php
The region template is used when a page region has content, either from the Block system or a function like hook_page_build(). Possible region names are determined by the theme's .info file.
- 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.
Cache issue
When working with template suggestion, there is a possibility that Drupal use its cache rather than the new templates as suggested. Remove the cache if you experience this problem. To clear the cache, choose one of the methods described in Clearing Drupal's cache.
Comments
Putting template files in custom module directory
I'm working on a custom module and I want to keep my template file in my module directory (as opposed to a theme directory)
This approach worked for me: http://www.metachunk.com/blog/adding-module-path-drupal-7-theme-registry...
To add page.tpl.php depending on content (node) type.
Use the following:
For D6:
<?phpfunction themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog" the template suggestion will be "page-blog.tpl.php".
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
}
}
?>
For D7:
<?phpfunction themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
}
}
?>
Perhaps Drupal 7 changed the
Perhaps Drupal 7 changed the name handling, because I did not need to do the string replacement. Otherwise this worked perfectly. The behavior that I saw with Drupal 7.2 was that the
theme_hook_suggestionsvariables contain all underscores, and that those underscores are all translated to hyphens when it looks to pick up the template file. Here's the code that worked for me:<?phpfunction themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}
?>
Thanks for you help. Cheers!
Node specific page layout in drupal 7
Following code worked for me in D7.
In this example i have specified 2 different layout page for 2 different Node.
page__contact will be converted to page--contact.tpl.php
page__about will be converted to page--about.tpl.php
So create 2 file inside your working named page--contact.tpl.php,page--about.tpl.php for 2 different layout.
*Replace "MYTHEME" with your working theme.
*Do not forget to Clear all caches
<?phpfunction MYTHEME_preprocess_page(&$variables, $hook) {
//Add multiple suggestions for pages based on Node
if(arg(1) == 3) { //For node 3
$variables['theme_hook_suggestions'][] = 'page__contact';
} if(arg(1) == 4) { //For node 4
$variables['theme_hook_suggestions'][] = 'page__about';
}
}
?>
This is great documentation!
This is great documentation! I'd note a few things that I found a bit of problems with...
Don't forget to add field to your name convention for most fields.. it's easy to miss.
Also, their isn't any documentation referencing taxonomy terms wonder if we can just rename them based on specific vocabularies?
Cheers
for taxonomy you can
for taxonomy you can use:
taxonomy-term--[vocabulary_machine_name].tpl.php
taxonomy-term--[tid].tpl.php
So...
So if I wanted a page template for all nodes/comments of a forum node type, would I change the code:
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
To
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->forum;
Thanks, I'm a noob.
ckmedia.ca
This code here worked good
This code here worked good for me
function THEMENAME_preprocess_page(&$variables, $hook) {
// Page template suggestions based off of content types
if (isset($variables['node'])) {
$variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
$variables['theme_hook_suggestions'][] = "page__node__" . $variables['node']->nid;
}
// Page template suggestions based off URL alias
if (module_exists('path')) {
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '__' . $path_part;
$variables['theme_hook_suggestions'][] = $template_filename;
}
}
}
}
Ubercart Promo Website - Cart Galleries - Module Listings - Forums
This worked for me as well
@ future assassin: Thanks!!
No problem took me a while to
No problem took me a while to find it online but so far its worked perfectly. I should have added a link to where I found it http://forrst.com/posts/Add_page_template_suggestions_depending_on_conte...
Ubercart Promo Website - Cart Galleries - Module Listings - Forums
The following helped answer
The following helped answer my question about overriding the node.tpl.php file with long content type names in Drupal 7:
In Drupal 6, some template files could be overridden in a targeted way. For example, the theme could contain a "node-article.tpl.php" file which would be used for nodes of the article type only. In Drupal 7, these need to be renamed to use a double-hyphen. For example, "node--article.tpl.php". A single hyphen is still used to separate words: for example, "user-picture.tpl.php" or "node--long-content-type-name.tpl.php", so the double hyphen always indicates a more targeted override of what comes before the "--". As another example, in Drupal 7 the override of a page.tpl.php for node/17 is page--node--17.tpl.php (in Drupal 6 it would have been page-node-17.tpl.php).
comment template does not work.
comment--[node-type].tpl.php
-drupal 7.9 installation.
comment.tpl.php works. however I have a content type 'media' that I created comment--node-media.tpl.php it never works. I tried it in many different ways like comment-node-media.tpl.php, comment-node--media.tpl.php, etc. not working.
Please advise me to use comment template.
Thanks in advance.
Did you try
Did you try "comment--media.tpl.php"? For me, if my content type's machine name is "xyz", then:
1. for the comment wrapper template I have to do: "comment-wrapper--node-xyz.tpl.php".
2. But for the comment template itself I have to do: "comment--xyz.tpl.php". Note the lack of the word "node".
Ive got a bug raised in core
Ive got a bug raised in core that seems to be confirmed at the moment:
http://drupal.org/node/1392494
but I am intrigued as to how yours is working when mine isnt....
Using Drupal 7.10. Actually
Using Drupal 7.10. Actually it's not working. I had been making so many changes to my site's theme functions and templates that I thought I was using a custom comment--xyz.tpl.php when in fact this file was an exact duplicate of the base comment.tpl.php. I just tried editing it now and my changes don't show up (with theme registry rebuilding on). The comment-wrapper override does work however.
thanks for that. comment
thanks for that.
comment wrapper per content type is
eg. article content type
comment-wrapper--node-article.tpl.phpUnderscored machine names do not get hyphenated
To extend upon http://drupal.org/node/1089656#comment-5408170:
If you have a module called my_module, the template theming blocks for that module is block--my_module.tpl.php. The underscore in "my_module" does not become a hyphen.
In block names (deltas), however, underscores do become hyphens. The template for theming the "my_block" block in the "my_module" module is block--my_module--my-block.tpl.php.