Drupal Themes: Breaking files out into sub-folders
While working on large Drupal installations theme folders can get very large. If you have numerous whatever.tpl.php files it can become quite confusing to locate the template file you need to edit when you need to make a change to your site. On top of that, if you are adding a lot of additional variables through pre-process functions (you are doing this in preprocess functions right, not in the tpl's...) your template.php file can become quite large as well. To remedy this there are two very simple things you can do to help organize your files and develop a theme structure that is much easier to maintain.
Tpl subfolders
All of your tpl files can be moved into sub-folders if you wish and Drupal should still pick them up. One structure that may make sense is to create a node, block, page, region, and views folder as these are the most common templates a site will use. Once you have these folders created you can move your tpl files into them accordingsly. The catch here is that you need to remember to clear the sites cache after doing this as Drupal will become confused if you move things around.
Sub template.php Files
With multiple preprocess functions going on your template.php file may become quite large. Sometimes it is much easier to break preprocess functions out into separate files for their specific purpose. For example, you may end up with large amounts of views preprocess functions. Often I separate these into a specific template.views.php file. For this to work you basically use the main template.php file to require all of the sub template.whatever.php files you have created. Below is some sample code:
<?php
/**
* Include our template.whatever.php files
*/
$theme_path = drupal_get_path('theme', 'your_theme');
// Specify the path to our template.php files
// I decided that my template.php files will
// reside in a folder named "php"
$filePath = "$theme_path/php";
// Open the directory
$dir = opendir($filePath);
while ($file = readdir($dir)) {
// require all files that end with a .php extension.
if (preg_match('/\.php/',$file)) {
require_once "$theme_path/php/$file";
}
}
?>The above code becomes the only thing that resides within the template.php file of your theme and all of your preprocess functions can then live within there sub template.php files.
