Last updated September 27, 2009. Created on December 19, 2006.
Edited by iva2k, SLIU, HansBKK, add1sun. 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,
      ),
   ),

  );
}

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

joshden’s picture

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.

  1. Enable the Path core module so pages can be arbitrarily given any URL path when created.
  2. Create a new content type called blank_page (or whatever). You will want to disable all comments and probably not have it promoted to the front page.
  3. Add this code to the very beginning of your page.tpl.php file:
    <?php
    if ($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">

    Change "blank_page" to the machine name of the content type you created. The arg(2) != "edit" allows this block of code to NOT be executed when the path is something like node/[node_id]/edit. This allows you to see the edit page with the regular page template.

  4. Create a new blank_page node. Because only the body of the page will appear, the Title field is only used for managing the content later or displaying it in a list of nodes[1]. You will probably not want to display the body in any node lists as it should contain the full HTML page (e.g. <html>…</html>). Set the URL path to something that makes sense for you (e.g. blankpage.html).
  5. Load your newly created page: e.g. http:/{mysite.com}/blankpage.html. If you View Page Source in your browser, you will notice that only the body section of the node appears.
  6. [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.

gpk’s picture

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

mkmaroon’s picture

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?

ryansnow’s picture

I recommend using a combination of page and node types to template.

http://drupal.org/node/1089642

Anonymous’s picture

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!

cornel.eftimie’s picture

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.

robmil29’s picture

Remember you can always use the Theme Developer module to help you determine the best candidate template files and preprocess functions.

Nuk-The Gunner’s picture

How can use the above concepts in drupal 7 ? Has the way to code for different layouts in the same theme using different page.tpl.php's has changed ?

druvision’s picture

Try using double-dash instead of a single dash for the template file names. E.g. page--node--513.tpl.php

Amnon
-
Amnon Levav - Drupal Development Management Consultant
Druvision - Web Solution Experts

namjoosadr’s picture

for front page page-front.tpl.php donot work me but page--front.tpl.php be true.

Science is everything.