Hello everyone,

I want to theme a nodes by it's ID.
In other words i would like to create template files like node-1.tpl.php, node-2.tpl.php, ...

Trying to do this i defined the theme preprocess function like this:

function THEME_NAME_preprocess(&$variables, $hook) {
	switch($hook) {
		case 'node':
			$variables['template_files'][] = 'node-' . $variables['nid'];
		break;
	}
	return $variables;
}

But this doesn't work.

I've been searching around in the theme.inc file to understand the steps drupal goes trough in choosing the template file.

The only thing i've found is that drupal only looks in the "modules/node" directory for that file and not in my theme directory.

Now my question is:
How can i tell drupal to look in the theme directory for that file?

Comments

nevets’s picture

This

            $variables['template_files'][] = 'node-' . $variables['nid'];

should be

            $node = $variables['node'];
            $variables['template_files'][] = 'node-' . $node->nid;
CoolCow’s picture

Tried it, but doesn't make a difference.

adiatis’s picture

There are some errors in variables. The following code works.

template.php:

<?php
function phptemplate_preprocess(&$vars, $hook) {
	switch ($hook){
		case 'node':
			$node_template = array_pop($vars['template_files']);
            $vars['template_files'][] = 'node-' . $vars['node']->nid;
            $vars['template_files'][]  = $node_template;
        break;
	}
	return $vars;
}
?>
Jeff Burnz’s picture

Just use the specific function - template_preprocess_node...

function themename_preprocess_node(&$vars, $hook) {
  $vars['template_files'][] = 'node-'.$vars['node']->nid;
}