Customizing the full page layout and sections based on path when using aliased paths
Last modified: June 20, 2009 - 18:19
description
This snippet allows you to customise the entire page layouts depending on the PATH when you are using the path.module (url alias).
usage
The snippet tells Drupal to look for custom page layouts in the current theme folder, based on the aliased path. For example, if the current page has an alias of discography/albums/revolver, you can have custom page.tpl.php files called:
- page-discography-albums-revolver.tpl.php
- page-discography-albums.tpl.php
- page-discography.tpl.php
Whichever layout file is found first is the one that will be used.
Step 1 of 1
- Using a text editor like notepad.exe or equivalent, copy the snippet below into your template.php file
- Upload your new/edited template.php file to the active theme folder.
- Upload your custom page_path_alias.tpl.php layout files to the active theme folder.
<?php
/**
* This snippet loads up different page-custom.tpl.php layout
* files automatically based on path when using the path.module
* and aliased paths
*
* This works with Drupal 4.7 and Drupal 5.x
*/
<?php
function _phptemplate_variables($hook, $variables = array()) {
switch ($hook) {
case 'page':
if (module_exists('path')) {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$custom_layouts = array();
$layout_filename = 'page';
foreach (explode('/', $alias) as $path_name) {
$layout_filename = $layout_filename . '-' . $path_name;
$custom_layouts[] = $layout_filename;
}
}
$variables['template_files'] = $custom_layouts;
}
break;
}
return $variables;
}
?>
Not working with modules i18n or localizer installed
This snippet doesn't work with drupal 5.x and module i18n installed and working or localizer (3.x version), maybe because of the changed url alias settings.
Example: after installing i18n module previously working page_friends.tpl.php won't be used anymore by page: mywebsite/friends
Renaming page_friends.tpl.php to page-en_friends.tpl.php (or page_en_friend.tpl.php) won't fix that.
enrico
http://www.enricolippi.net