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
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,
),
),
);
}
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
but php snipets don't seem to work this way?
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
<?phpif ($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
What if we do not use clean urls?
I see, that this example is using clean urls. Is it the same way when I do not use clean urls?
I need it specially to set different title image for each language.
Clean URLs are merely a
Clean URLs are merely a cosmetic feature. Drupal considers the path to be the same whether they are on or off.
i've just test my website in
i've just test my website in ie8 (under windows 7 + vmware)
huge problem when i try to login
user page (page-user.tpl.php) is not working anymore
so what i get is blank screen
any idea why is this happen?
thanks, best regards, erwin
Does the following apply to
Does the following apply to Drupal 6x? If so, I'm wondering if we should say that in the text above and cross-reference it within the path-auto module and its help file:
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.