4

I've just made the jump from WordPress to Drupal, and though I love what I'm seeing there's a basic piece of functionality that's obvious in WordPress but is escaping me in Drupal- How does one, for a given page or piece of content, assign it to a specific template?

Please note that I'm not trying to design a template for a specific content type, rather I want the ability to choose a template at the individual content/node level, in the edit screen.

It doesn't look like this is part of out-of-the-box Drupal, so does anyone have suggestions as to how to go about implementing it?

7

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.

  • @marcvangend- Thanks, I think this is what I'm looking for- Haven't yet played around w vocabularies but it sounds like the right way to go- – Yarin Oct 16 '11 at 13:01
  • You're welcome. Taxonomy (vocabularies & terms) is such a flexible tool, you will not regret learning about it! – marcvangend Oct 16 '11 at 19:00
4

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.

  • Kaivosukeltaja- Thanks, good find- If this is the best thing out there then maybe I'll just have to do the port myself-- anyway, I'll give you the answer unless someone comes up with something more current. – Yarin Oct 15 '11 at 19:02
  • 1
    The issue for a Drupal 7 port of Custom Node Template is at drupal.org/node/1035288 – Pierre Buyle Oct 16 '11 at 8:17
3

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.

  • How far along is this module? I'm hesitant to use it for my production site since it's still in alpha.. That being said, it looks awesome, and exactly what I need. – Rich Sep 27 '13 at 5:49
  • I've never had issues. I sent it out there as an alpha release, because I didn't know if there were any issues I wasn't aware of. I should probably upgrade it soon since it's been 6 months with no major issues reported. – bryanbraun Oct 1 '13 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.