Note to self: Drupal Theming trick

while learning more on getting templates up for drupal, i notice i forgot this while upgrading cy's site. But a note to self post will help me to remember the next time i update drupal :)

in the guide under http://drupal.org/node/139766, a trick is given to allow one to have custom theme differentiated by url path names. So only thing you and i must do is to add the following function in a file called template.php in your own theme folderto the phptemplate.engine file.

<?php
function _phptemplate_variables($hook, $vars = array()) {
switch (
$hook) {
case
'page':

// Add page template suggestions based on the aliased path.
// For instance, if the current page has an alias of about/history/early,
// we'll have templates of:
// page-about-history-early.tpl.php
// page-about-history.tpl.php
// page-about.tpl.php
// Whichever is found first is the one that will be used.
if (module_exists('path')) {
$alias = drupal_get_path_alias($_GET['q']);
if (
$alias != $_GET['q']) {
$suggestions = array();
$template_filename = 'page';
foreach (
explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
}
$vars['template_files'] = $suggestions;
}
break;
}

return $vars;
}
?>

and this should do the job. For example, if all pages under http://wahlau.org/jokes should use a different theme, i only need to add an extra page-jokes.tpl.php with the different design in my theme path, and i should be able to have all contents under jokes using page-jokes.tpl.php instead of the general page.tpl.php.

for me this is a neat feature - i don't need multi theme since i only want to have certain pages to have a different look, and i can set those pages under the same sub path (they don even need to really exist, url aliases will work) as long as the contents make sense under that sub path, this approach will serve well.

If you are into customizing drupal, do read through the guide. It has really good tips and tricks that will help you customizing your drupal sites.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Peace Be Upon You

Oh Wahlau,

Greetings. Need help, not me, one of my boys. Please email me.

HYWan

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.