Separate theme for a specific node

If you want to use a specific node.tpl.php for a node, paste or merge this code into your template.php:

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'node':
     
$vars['template_files'] = array('node-'. $vars['nid']);
      break;
  }
  return
$vars;
}
?>

You can now for example create files named node-34.tpl.php to create a custom theme for just the node 34.

Drupal 6 solution:

yelloroadie - May 11, 2008 - 23:22

This method does not work in Drupal 6 as _phptemplate_variables() is no longer available.

I tried inserting this code into my template.php file:

<?php
function phptemplate_preprocess_node(&$vars) {
   
$vars['template_files'] = array('node-'. $vars['nid']);
    return
$vars;
}
?>

Then you can then name node specific template variables as in the above article - but then you can no longer theme by content type... so it's not a proper solution...

After fiddling around for a while, I got something better:

<?php
//allows node specific themes.
function phptemplate_preprocess_node(&$variables) {
   
$potentialNodeSpecificTemplate = 'node-'. $variables['nid'];
   
//check if there is a node specific template.
   
$potentialNodeSpecificTemplateFile = path_to_theme()."/".$potentialNodeSpecificTemplate.".tpl.php";
    if (
file_exists($potentialNodeSpecificTemplateFile)) {
       
//use this template file.
       
$variables['template_files'] = array($potentialNodeSpecificTemplate);
    }
    return
$variables;
}
?>

This function checks to see if there is a node specific template file and if it finds one loads it. If it isn't there it does nothing, thus solving the problem the first function encountered.

Better

gpk - June 11, 2008 - 13:57

<?php
function phptemplate_preprocess_node(&$vars) {
   
$vars['template_files'][] = array('node-'. $vars['nid']);
    return
$vars;
}
?>

This avoids overwriting the existing content of $vars['template_files'] (which should be an array containing 'node-nodetype'), and hence the more complex code is then not needed. Actually, Drupal 6 info on template suggestions is here ... http://drupal.org/node/223440

gpk
----
www.alexoria.co.uk

 
 

Drupal is a registered trademark of Dries Buytaert.