Using different page templates depending on the current path

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

Note that the front page is a special case (that is the URL is http://example.com with no path following.) In this case you can use a special template:

page-front.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.

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.

Creating a Blank Page Content Type

joshden - January 2, 2008 - 05:33

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.

[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

gpk - January 9, 2008 - 11:36

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

but php snipets don't seem to work this way?

scottrigby - January 16, 2008 - 17:31

This a great tip - but I wasn't able to get php to work (for instance, adding a login form, or showing the $tabs).

So instead I tried printing $content like this

<?php
if ($node->type == "splash" && arg(2) != "edit") {
  print(
$content);
  return;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

This way it still prints the body, and it prints the php snippets (though oddly it didn't print the $tabs, which I inluded in my html the same way it shows in the page template). This way also places the html head within a node div (unlike your example to print $node->body), which is not ideal.

Is there a better way to do modify your code to allow php snippets? or am I missing something?

Thanks!
Scott

Theming nodes by content type

gagarine - February 13, 2008 - 13:07

Here the page for Theming nodes by content type.

Issue with Drupal 6.1?

dealt - March 4, 2008 - 17:06

Is there an issue with this page templates using path/url alias with drupal 6.1? It's strange because I can make a custom template for Administration pages (page-admin.tpl.php) and although it worked for user-created pages like About Us page (using page-node-1.tpl.php), it won't on path/url aliases like page-about.tpl.php . Even with theme developer, it reports Candidate template files: page-node-3.tpl.php < page-node.tpl.php < page.tpl.php and do not include URL/path alias. Did I miss something? Thanks

I don't think different page

gpk - March 4, 2008 - 23:23

I don't think different page templates per URL alias are supported out-of-the-box in any version. See e.g. http://drupal.org/node/139766. In 6.x I think you have to use the "preprocess" thing instead of _phptemplate_variables() ...

gpk
----
www.alexoria.co.uk

 
 

Drupal is a registered trademark of Dries Buytaert.