Chapter Three LLC

HOWTO: Use Different Node Templates by Node Variables (nid, type, view)

Farsheed Hamidi-Toosi

With node template files you are often limited to something like node.tpl.php and node-blog.tpl.php. Often times it’d be nice to make a different template for just one specific node or a different template for teaser/list view and full node view.

Using the PHP code below, Drupal will look for these template files, split by page or no page view. This gives more fine grained control over your node tpl.php files.

Page view:

1) node-[nid]-page.tpl.php
   Node by itself on a page, specific NID
2) node-[type]-page.tpl.php
   Node by itself on a page, specific type
3) node-default-page.tpl.php
  Node by itself on a page, default

Listing/Teaser view:

4) node-[nid].tpl.php
   Node in list/teaser, specific NID
5) node-[type].tpl.php
   Node in list/teaser, specific type
6) node.tpl.php
   Node in list/teaser, default.

*UPDATE* Earl Miles has shown this can be done much simpler than what I had previously. Revised code below.

Place this code in your template.php file in your theme’s directory.

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'node':
     
// Here is the way to switch to a different node-<something> template based on node properties.
     
if ($vars['page']) {
       
// This is LIFO (Last In First Out) so put them in reverse order, i.e
        // most important last.
       
$vars['template_files'] = array('node-default-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page');
      }
      else {
       
$vars['template_files'] = array('node-'. $vars['node']->nid);
      }
      break;
  }

  return
$vars;
}
?>

we are a group of dedicated

we are a group of dedicated players who have come together to provide a service for the community so that you, our fellow player,We offer wow gold and aoc gold,our website is wow gold Professional offered,please buy lotro gold in www.storeingame.com.
集装袋

Posted by Anonymous (not verified) | Sep. 4th, 2008 @ 10:20pm | Link to this Comment

Very Cool

This is amazing. I have been looking for something like this to use on my Visual Basic Source site. Currently I have different node types for source code samples, tutorials, articles, forum posts, etc. However, It is a pain to develop any custom looking display for each type. Now using your example I can put it all in separate template files. Very helpful. Thank you so much.

Posted by VB6 (not verified) | Sep. 3rd, 2008 @ 3:17pm | Link to this Comment

Thank you very

Thank you very much.

————
sohbet

Posted by Anonymous (not verified) | Aug. 5th, 2008 @ 11:38am | Link to this Comment

Nice explanation. Very

Nice explanation.

Very similar things can be done with blocks, regions …
See http://drupal.org/node/104319

Posted by themegarden.org (not verified) | Jun. 9th, 2007 @ 2:11am | Link to this Comment

I also changed the fallback

I also changed the fallback for the page view to node-default-page.tpl.php since node-page.tpl.php overlaps with the “page” node style.

Posted by Farsheed Hamidi... | May. 15th, 2007 @ 1:22pm | Link to this Comment

Great post mate, I’m

Great post mate, I’m learning so much from your site this week :D

Posted by aoc gold (not verified) | Aug. 19th, 2008 @ 1:10am | Link to this Comment

Thanks Earl! I didn’t know

Thanks Earl! I didn’t know $vars[‘template_files’] was available for the node case. I’ve updated the main post.

Posted by zirafa (not verified) | May. 15th, 2007 @ 8:41am | Link to this Comment

You're doing too much!

While I haven’t tested this directly, something along these lines should do the trick just as well and be even easier.

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'node':
     
// Here is the way to switch to a different node-<something> template based on node properties.
     
if ($vars['page']) {
       
// This is LIFO (Last In First Out) so put them in reverse order, i.e
        // most important last.
       
vars['template_files'] = array('node-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page');
      }
      else {
       
vars['template_files'] = array('node-'. $vars['node']->nid);
      }
      break;
  }

  return
$vars;
}
?>

Posted by merlinofchaos (not verified) | May. 14th, 2007 @ 6:45pm | Link to this Comment

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br/> <br /> <p> <img> <blockquote> <i> <b> <u>
  • Lines and paragraphs break automatically.
  • SmartyPants will translate ASCII punctuation characters into “smart” typographic punctuation HTML entities.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options