Switching Drupal tpl.php files at will: Old Switchy McTipplefep's Trick
Section:
Tags:
One of the first lessons they teach you at the School of Drupal Arts, Arcane Sciences and Sorcery is that a tpl.php can have dynamically generated wildcards or "suggestions". You can see this every day drupal themes: you can simply use the "node.tpl.php" file if you want only one style for a node. Where as if you want different styles for three node types: blog, story & page, you create the following files:
- node-page.tpl.php
- node-story.tpl.php
- node-blog.tpl.php
- page-user-register.tpl.php
- page-user-edit.tpl.php
- wants 3 styles that are shared by 20 blocks
- wants 4 possible layouts for dozens of pages in a site
- has 12 node types, but wants them to share 3 styles
function example_preprocess_node(&$vars) { $node = $vars['node']; // only switch for story type switch($node->type) { case 'story': $vars['template_files'][] = 'node-batman'; break; } } function example_preprocess_block(&$vars) { // sure why not -- we'll pass our node.tpl.php file to our block // That's how tpl pimpin works sometimes $vars['template_files'][] = 'node-batman'; }