Using different “page.tpl” on Drupal 6.

Wed, Apr 7, 2010 by Erico

Often we are in a situation where we need one, or several pages of a website to have a completely different structure. When this happens you will most likely have to use a different page.tpl .

The simplest way to change your page.tpl is to use the website's URL to explain to Drupal what page.tpl it should be using. For example, to assign a different page.tpl to a page that is under “yourbasepath.com/splashpage” all you have to do is follow this one step process. Go into your theme folder, and create “page-splashpage.tpl.php”. Once Drupal recognizes the link (/splashpage), it will then know to use this specific .tpl file.

But what happens when you need to use the same template more then once? This is when a page preprocess will come in hand! Here is what we’re going to do:

On your template.php we’re going to preprocess the page. It looks something like this:


/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
//variables to be preprocessed
}

Now we’re going to use $vars['template_files'] to tell Drupal which file should be used on a specific page. In the following example we will be using “page-splashpage.tpl.php" for the user/register, and also for the user/password pages.


/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
if (arg(0) == 'user' && (arg(1) == 'register' || arg(1) == 'password')) {
$vars['template_files'][] = 'page-splashpage';
}
}

Now you can change the "if statement" to satisfy whatever condition you need, node types, paths, etc...

Have a happy theming!

nallavan posted on August 12, 2010 5:20 am

there is no function for maintanace.tpl.php in template.php

pasive posted on July 30, 2010 4:41 pm

Hi, I,ve tested both of them, and Denis`s solution worked for me, I might say it is more logical, because it uses drupal_get_path_alias, in case if you are using Not default node/*, it is correct solution. (I gets path set by admin).

Dennis posted on July 27, 2010 2:37 pm

I used this variant :

function phptemplate_preprocess_page(&$vars) {
$path = $_GET['q'];
$path = drupal_get_path_alias($path, $path_language = '');
$patharray = explode("/", $path);
if ($patharray[0] == 'news') {
$vars['template_files'][] = 'page-node-news_page';
}
}

Dennis posted on July 27, 2010 2:32 pm

arg(0) says 'node' instead of 'user'

Anonymous posted on June 27, 2010 11:15 pm

I'm really just getting started with Drupal...

So if I set up 2 new page.tpl.php files: page-news.tpl.php & page-careers.tpl.php, is there a standard URL that I can access them at?

admin posted on May 12, 2010 5:30 pm

@sushilhanwate you could try this:
/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
if (arg(0) == 'user' && arg(2) == 'messages' ) {
$vars['template_files'][] = 'page-yourtemplatename';
}
}

not too sure about the arg(2) part, maybe you'll have to parse the URL write an if statement satisfy the condition of an url ending on /messages

sushilhanwate posted on May 7, 2010 12:54 pm

What is the solution for dynamic link like "mysite.com/user/1/messages"
Please Give solution if anyone has any idea.

Erico Nascimento posted on April 13, 2010 10:45 pm

For a maintenance page Drupal uses maintenance-page.tpl.php . Take a look on garland to see how it is done. It has some default behaviours ready for you.

Then you can theme it similar to your page.tpl

Regarding the symbolic link I've never used this technique.

Cheers!

seed posted on April 12, 2010 8:06 am

for a maintenance page what is the best solution? using a .tpl file or template.php?

Irakli Nadareishvili posted on April 10, 2010 7:23 pm

You should look into Context3's Context Layout module.

wuf31 posted on April 8, 2010 3:22 pm

Interesting technique, but couldn't we just use use symbolic link here ?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <h2>
  • Lines and paragraphs break automatically.

More information about formatting options