Customising the full page layout and sections based on node type

Last modified: July 16, 2008 - 17:09

If you are using Drupal 5, follow the directions on this page instead: Different page templates depending on node type

description

This explains how to customise the entire page layout depending on the node type. An example application is if you wanted your entire blog pages to look different to your book pages.

usage

As an illustrative example, the following step-by-step approach shows you how to setup different (full) page layouts for your blogs, books and the front page to your site.

Step 1 of 2

  1. make a copy of your page.tpl.php file and rename it to be page-default.tpl.php.
  2. make further copies your page.tpl.php file it and rename them to be page-front.tpl.php, page-blog.tpl.php and page-book.tpl.php etcetera..
  3. using a text editor like notepad.exe or equivalent, modify the layout of each tpl.php file to suit your desires
  4. upload your new page-type.tpl.php layout files to your active theme folder

Step 2 of 2

  1. Using a text editor like notepad.exe or equivalent, replace the contents of your page.tpl.php file with the snippet below
  2. Ensure that you have a page-default.tpl.php file as part of your collection of layouts.
  3. Upload your new page.tpl.php file to your active theme folder and your new layouts will take effect automatically

<?php

/**
* This snippet loads up different page-type.tpl.php layout
* files automatically. For use in a page.tpl.php file.
*
* This works with Drupal 4.5,  Drupal 4.6 and Drupal 4.7
*/

if ($is_front) {/* check if it's the front page */
   
include 'page-front.tpl.php'; /*load a custom front-page.tpl.php */
   
return; }

if (
$node->type == 'book') {/* check if it's a book page */
   
include 'page-book.tpl.php'; /*load a page-book.tpl.php */
   
return; }

if (
$node->type == 'blog') {/* check if it's a blog node */
   
include 'page-blog.tpl.php'; /*load  page-blog.tpl.php */
   
return; }

if (
$node->type == 'image') {/* check if it's an image node */
   
include 'page-image.tpl.php'; /*load page-image.tpl.php */
   
return; }

if (
$node->type == 'forum') {/* check if it's a forum node */
   
include 'page-forum.tpl.php'; /*load page-forum.tpl.php */
   
return; }

include
'page-default.tpl.php'; /*if none of the above applies, load the page-default.tpl.php */
   
return;

?>

Adding Node Type to the Overrides

DanDaMan - August 20, 2007 - 17:27

This code is a bit cumbersome and requires a bunch of changes to override. I found this solution to work better. In your template.php file, you can add a file name to the list of available overrides based on the node_type variable. Just put this code in your template.php file and then you can give a different template to page-node-node_type.tpl.php.

<?php
/**
* Override or insert PHPTemplate variables into the templates.
*/
function _phptemplate_variables($hook, $vars) {
  if (
$hook == 'page') {

   
// This first part was already in the PHPTemplate engine code and gives page.tpl.php variances based on the args.
   
$i = 0;
   
$suggestion = 'page';
   
$suggestions = array($suggestion);
    while (
$arg = arg($i++)) {
     
$suggestions[] = $suggestion . '-' . $arg;
      if (!
is_numeric($arg)) {
       
$suggestion .= '-' . $arg;
      }
    }
   
// This if block was added to give a page-node-node_type.tpl.php that has precedence over the args.
   
if (arg(0) == 'node' && is_numeric(arg(1)) && isset($vars['node'])) {
      if (isset(
$vars['node']->type)) {
       
$suggestions[] = $suggestion . '-' . $vars['node']->type;
      }
    }
   
// This also from PHPTemplate to make sure the front page gets its own template.
   
if (drupal_is_front_page()) {
     
$suggestions[] = 'page-front';
    }
     
   
$vars['template_files'] = $suggestions;
     
    return
$vars;
  }
  return array();
}
?>

If you move the if (arg(0) == 'node'...) block of code to above the while, then it will look for the argument-based page templates first then fall back to the node_type templates. Finally, of course, it will default to page.tpl.php.

Dan "da Man"
Webmaster
http://cMusicWeb.com/
http://da-Man.com/

For Garland based themes

chris_five - January 8, 2008 - 18:41

... the abovementioned snippet can be placed just before the break statement of the "page" case in the $hook switch in function _phptemplate_variables().

I found this snippet to be very useful and it saved me a bunch of time. Thanks!

An Even Simpler Way

WisTex - February 17, 2008 - 23:59

If you are doing something really simple, such as displaying a Google Adsense ad in a story, but not in other node types, there is an even easier way to do it. No reason for creating separate template pages if all you need is a simple if then statement. (Note: If you are doing something more complicated, then the previous suggestions are probably a better way to do it.)

So here goes the simple if then solution:

Just before it says

<?php print $content; ?>

in page.tpl.php, insert the following code (with proper modifications of course):

<?php if ($node->type == 'story'):?>
<div style="display:block;float:right;margin:5px 0px 5px 5px;">
<!-- paste your Google Adsense code here -->
</div>
<?php endif; ?>

This would insert an ad that floats to the upper right of the story and the story text would wrap itself around the ad. It would automatically adjust based on screen resolution.

Of course, this could be used for other things, such as minor template changes that would amount to a few lines, where it is not worth the time to duplicate the template file multiple times just for a single line change.

Hope that helps,

Scott

--
Scott M. Stolz
http://www.wistex.com/

 
 

Drupal is a registered trademark of Dries Buytaert.