2

Up to now, I've always hard coded what page template a certain page should use, either based on the URL, or what type of node it is.

What would be really useful is if there was a way to select which tpl file to use, right there in the node edit form. This would allow the user to flick between different page layouts at will.

Does anyone know a good way to approach this problem, or a flat out solution to it?

apaderno
  • 28,547
  • 16
  • 75
  • 90
jsims281
  • 2,206
  • 2
  • 30
  • 57

3 Answers3

2

ThemeKey will let you load a theme based on a path or other criteria. You can use other methods like utilize preprocesser functions of template.php, and hook it in with hook_form_alter and come up with a way to switch files.

Kevin
  • 13,153
  • 11
  • 60
  • 87
  • Yes, I think I will add a CCK field populated with possible templates, then use a pre processor function to choose the theme template based on that field. – jsims281 Aug 03 '10 at 11:33
  • 1
    A simple approach would be to set a taxonomy vocabulary with the following terms: Theme A, Theme B, Theme C etc. Configure ThemeKey to switch depending on the taxonomy term. And you're done! – Sid Kshatriya Aug 03 '10 at 21:11
1

I ended up adding a new vocabulary for template files (the VID for this is 2 in my case) , and then rolled this into the page preprocessor in my template.php:

function phptemplate_preprocess_page(&$vars) {                                              

  if (count($vars[node]->taxonomy)>0) 
    foreach ($vars[node]->taxonomy as $term) 
      $template = $term->vid == 2 ? $term->name : NULL; 

  if ($template) $vars['template_files'][] = "template-".preg_replace("/[^a-zA-Z0-9s]/", "", strtolower($template));  

}

Now if I have a node in a taxonomy term called: A Green Page! it will look for template-agreenpage.tpl.php as a template file.

jsims281
  • 2,206
  • 2
  • 30
  • 57
0

I was wanting this functionality as well, so I made a module to do this very thing for node templates. You can find it here: http://drupal.org/project/template-picker

bryanbraun
  • 3,025
  • 2
  • 26
  • 38