Last updated September 27, 2009. Created by HansBKK on December 19, 2006.
Edited by iva2k, SLIU, add1sun, joachim. Log in to edit this page.
In Drupal 5.0, PHPTemplate supports the use of multiple page templates for a single theme. Depending on the current url path (node/1, taxonomy/term/2, or user/1, for example), PHPTemplate will search for multiple templates before falling back on the default page.tpl.php file.
For example, if you were to visit http://www.example.com/node/1/edit, PHPtemplate would look for the following templates, in descending order:
page-node-edit.tpl.php
page-node-1.tpl.php
page-node.tpl.php
page.tpl.php
If you were to visit http://www.example.com/tracker, PHPTemplate would look for the following templates:
page-tracker.tpl.php
page.tpl.php
This rule also works for URLs for things like users and taxonomy. When visiting http://www.example.com/user/1, PHPTemplate looks for the following template files in this order:
page-user-1.tpl.php
page-user.tpl.php
page.tpl.php
Remember that these template suggestions are based on the default drupal path for a particular page. If you've used the path or pathauto module to hide them with url aliases, these templates will still be searched based on the original paths. See Different page templates depending on URL aliases for more information on how to use URL aliases for templates.
Note that the front page is a special case: the special template:
page-front.tpl.php
will be used for the page set to , or the URL http://example.com with no path following, as well as any path aliased to that location - in other words this is the same as the 'is_front' variable, so using this as a conditional test in your main page template may be easier to maintain.
If you need to switch page template files based on some other rule (the role of the logged in user, for example), implement the phptemplate_variables() function in your theme's template.php file. The $vars['template_files'] variable should store an array of possible tpl.php files, with the first one to check listed last.
Note for module-provided templates: A module can provide its own page template files. It is done in hook_theme() function:
/*
* Implementation of hook_theme().
*/
function mymodule_theme() {
return array(
'page' => array(
'template' => 'page-node-edit',
'arguments' => array(
'content' => NULL,
'show_blocks' => TRUE,
'show_messages' => TRUE,
),
),
);
}
Comments
Creating a Blank Page Content Type
I have a need to create pages on my website that only contain the HTML entered in the Body field of the page. Because using mod_rewrite (in Apache) causes Drupal to serve all requests to {mysite.com}/anything, I needed a way to create static HTML pages that emulates the old way (similar to uploading testfile.html to /home/my_user/public_html). This method provides that capability.
<?phpif ($node->type == "blank_page" && arg(2) != "edit") {
print $node->body;
return;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
[1]If you use the Pathauto module, you could set the pattern for the blank_page content type to use the title in it. That way you're not completely wasting the title field. You could use something like this: [title-raw].html
Thanks!
Josh
P.S. I am very new to Drupal, so use this code at your own risk. Please let me know if you have a better way of accomplishing this task.
Note that you can still
Note that you can still upload an HTML file the old way using FTP or whatever - Drupal will only handle a request if the request doesn't resolve to an existing file.
Still a useful tip though :-)
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Okay so I am using drupal 6,
Okay so I am using drupal 6, but I can't find the relevant section there, but what i'm doing is exactly whats mentioned in this article, and i'm having a slight problem i'm not sure how to get around.
So i want to template a 'section' path like this:
mysite.com/section
i want all the pages in the path to have the same look, ex:
/section/page1
/section/story1
/section/something
so I create a page-section.tpl.php
however, i want the SECTION page itself to have a slight modification, so I need a different tpl.php file for the page at /section
but I can't figure out the best way to do this. I found out that the node id for /section is 8, so i created a tpl file called page-node-8.tpl.php, but it's still using the template from page-section.tpl.php . (ie. when there's page-section.tpl.php AND page-node-8.tpl.php, it's using page-section.tpl.php, if there's not page-section.tpl.php, then it uses page-node-8.tpl.php).
I hope I have explained clearly. I hope someone might be able to help me?
Node & Page
I recommend using a combination of page and node types to template.
http://drupal.org/node/1089642
Won't this be overwritten on update?
Silly question? Maybe. :) But I was wondering if you add or change page.tpl.php, won't that be overwritten when your theme needs a security update? I'm using this to replace the primary links menu with one using the Megamenus module, but want to make sure I do this properly so I won't have to fix it every single time we update theme. Thanks!
Yes, it will be
That is you shouldn't modify templates directly in the theme. You should instead create a subtheme and put your modified templates there. They will override the ones in the theme.
Theme Developer
Remember you can always use the Theme Developer module to help you determine the best candidate template files and preprocess functions.
Create & Download Custom Ringtones and Mobile Phone Backgrounds