Search code examples
drupaltemplatesdrupal-6themesdrupal-theming

Select a template through the admin area?


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?


Solution

  • 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.