I need some help on combing the code on the following pages so that its possible to switch both node-alias.tpl.php and page-alias.tpl.php templates

Different page templates depending on URL aliases
http://drupal.org/node/139766

and

Different node templates depending on URL aliases
http://drupal.org/node/117491

Thanks
Arek

Comments

nevets’s picture

I thiink this will do the trick though it untested

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
   
      // Add both page and node 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
      // node_about_history_early.tpl.php
      // node_about_history.tpl.php
      // node_about.tpl.php
      // Whichever approriate one 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';
          $template2_filename = 'node';
          foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '_' . $path_part;
            $suggestions[] = $template_filename;
            $template2_filename = $template2_filename . '_' . $path_part;
            $suggestions[] = $template2_filename;
          }
        }
        $vars['template_files'] = $suggestions;
      }
      break;
  }
 
  return $vars;
}
?>
end user’s picture

Hi

Works for page_template1.tpl.php but when I create a node_template1.tpl.php the template (page_template1.tpl.php) is not read and stripped out and only shows the node-template1.tpl.php as the main template.

Thanks
Arek

nevets’s picture

II should have read the node example a little closer.
How about this

<?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;

    case 'node':
   
      // Add node 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:
      // node_about_history_early.tpl.php
      // node_about_history.tpl.php
      // node_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 = 'node';
          foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '_' . $path_part;
            $suggestions[] = $template_filename;
          }
        }
        $vars['template_files'] = $suggestions;
      }
      break;
  }
 
  return $vars;
}
?>
end user’s picture

Haven t had time to test this out as I went with the taxonomy theme way but will try it out later today.

end user’s picture

Tried it and it works so far. Even works with the Taxonomy Theme module.

end user’s picture

I had a problem with Taxonomy Theme not showing some blocks on certain themes. I got rid of Taxonomy Theme and I've switched to using the code posted here and its been working pretty good so far.