Customising the full page layout and sections based on path

description

This explains how to customise the entire page layout depending on the PATH and/or taxonomy terms.

update for Drupal 5.x

The PHPTEMPLATE Engine for Drupal 5.x has a new feature that automatically loads page-layout.tpl.php files as soon as you upload them to your active theme folder. In other words, if you upload a page-blog.tpl.php file to your active theme folder, you don't need to tell template.php.

Here's the notes from the phptemplate engine file:

Build a list of suggested template files in order of specificity. One
suggestion is made for every element of the current path, though
numeric elements are not carried to subsequent suggestions. For example,
www.example.com/node/1/edit would result in the following
suggestions:

Build a list of suggested template files in order of specificity. One
suggestion is made for every element of the current path, though
numeric elements are not carried to subsequent suggestions. For example,
www.example.com/node/1/edit would result in the following
suggestions:
 
  page-node-edit.tpl.php
  page-node-1.tpl.php
  page-node.tpl.php
  page.tpl.php

The following snippets may still be used with Drupal 5.x themes, in particular in situations where the phptemplate engine 'drops' the custom page-layout.tpl.php file.

For example, if you upload a custom page-forum.tpl.php file, the layout is 'dropped' when you're viewing a thread. Which is something you may not want.

For Drupal 4.7.x and earlier you need to instruct template.php to load a custom page-layout.tpl.php file.

usage

A simple example would be using the following path to tell Drupal to load a custom page-admin.tpl.php layout file.

e.g. If the current path is www.example.com/admin/* The arg(n) variable is therefore "admin" and we can use that to tell Drupal to load a page-admin.tpl.php.

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 (arg(0)=="admin") {/* check if the path is example.com/admin */
   
include 'page-admin.tpl.php'; /*load a custom page-admin.tpl.php */
   
return; }

if (
$node->type == 'blog') {/* check if the path is example.com/blog  */
   
include 'page-blog.tpl.php'; /*load page-blog.tpl.php */
   
return; }

/*insert more layout calls here before the call to page-default.tpl.php */

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

adding more layouts

The example snippet above should be self explanatory and includes snippets to load a custom page-admin.tpl.php and page-blog.tpl.php.

If you want to add more layout calls to your page.tpl.php file, copy this snippet and edit using the notes below.

if ($node->type == 'blog') {/* check if the path is example.com/blog  */
    include 'page-blog.tpl.php'; /*load page-blog.tpl.php */
    return; }

tips and more examples

Here are more examples of using the path that you can use to extend out your layouts even further.

arg(0)=="admin"// is /admin

arg(0) =="node"// is /node

arg(0)=="user" // is /user

arg(0)=="node" && arg(1)=="add" // is /node/add

arg(0)=="node" && arg(arg(2)=="edit" // is /node/###/edit

arg(0)=="user" && arg(1)=="add" // is /user/add

arg(0)=="admin" && arg(1)="user" && arg(2)=="create" // is /admin/user/create

arg(0)=="alias" && arg(1)=="alias1" is /alias/alias1

arg(0)=="taxonomy" && arg(1)=="term" && arg(2)=="term#" // is /taxonomy/term/term#

//arg(1)=="comment"

//arg(2)=="reply"

To keep it more inline with

dvessel - July 1, 2007 - 20:42

To keep it more inline with the theming engine (PHPTemplate), add to the template naming suggestions.

There are two vars you can pass from phptemplate_variables to have it included. (inside template.php)

<?php
function _phptemplate_variables($hook, $vars) {
 
// $hook == current callback.
 
if ($hook == 'page') {

    if (isset(
$vars['node'])) {
     
// "template_file" to check for a single template.
     
$vars['template_file'] = 'page-'. $vars['node']->type;
    }

   
// "template_files" to check for an array of templates.
   
$vars['template_files'][] = 'page-'. arg(0);
   
$vars['template_files'][] = 'page-'. $vars['layout'];
  }
 
  return
$vars;
}
?>

That would check for these templates in this order:
page-story.tpl.php (for node type of story rendering a in a full page)
page-left.tpl.php (if the current page is rendering with a left sidebar)
page-admin.tpl.php (when your browsing the admin pages)

You can pass whatever you like in there. It'll be scanned through and first found file in your template directory will be included. No need to manually include. see _phptemplate_callback to see how it works.

[edit: corrected the typos listed below.]

Error

newswatch - October 23, 2007 - 12:10

I am getting Parse error: syntax error, unexpected '{' in /home/wwatch/public_html/themes/themename/template.php on line 122

Line 122 is:
if (isset($vars['node']) {

change this line: if

blueflowers - October 23, 2007 - 17:36

change this line: if (isset($vars['node']) {
to: if (isset($vars['node'])) {

More changes?

abautu - December 11, 2007 - 11:53

To make it work (in Drupal 5.1) I've also had to add an underscore before the function name (_phptemplate_variables) and a semicolon after initialization of $vars['template_files'].

    $vars['template_files'] = array(
      'page-'. arg(0),
      'page-'. $vars['layout'],
    );

Works really nice and it's much cleaner that the original post solution.

Best wishes,
Andrei.

 
 

Drupal is a registered trademark of Dries Buytaert.