Drupal - How can I assign pages to templates like in WordPress?

What I would do, is to create a Vocabulary called "template" (using the Taxonomy module in Drupal core) and add some template names to it as taxonomy terms. This will allow you to 'tag' your nodes with the template name for that node.

Next, I would create a preprocess function for the node template in my theme's template.php file and add template suggestions based on the taxonomy term. Roughly the preprocess function would look like this (not working code, just an outline):

function MYTHEME_preprocess_node(&$variables) {
  // Get the Term ID from the "template" vocab for the current node.
  $template_tid = ... // (figure out where that data is in the $variables)
  if (!empty($template_tid)) {
    // Tell Drupal to use the node--[template_tid].tpl.php template if it exists.
    $variables['theme_hook_suggestion'] = 'node__' . $template_tid;
  }
}

Note that I'm suggesting to add template suggestions based on term ID, not term name. If you build it like this, you can always change the term names without breaking your template files.

For more information about template suggestions, see Working with template suggestions.


There's a module called Custom Node Template that lets you choose a template file on a node by node basis. Unfortunately, it only seems to be available for Drupal 6 and the author has stated he can't currently spare the time to port it to D7.


I was looking for a module like this a while back and couldn't find anything, so I started to work on a way to do it for Drupal 7, which became the Template Picker module.

I didn't discover the Custom Node Template module until afterwards, but that looks like a great option for Drupal 6.

Tags:

Theming

7