Force the use of tpl files for any theme function

davideads's picture

On a theme registry rebuild, this evil version of the theme registration function peeks in the theme path for a template file with the same name as the theme function (i.e. if somethemefunction is called, it looks for somethemefunction.tpl.php), and forces the template found to register for that theme call, no matter how it was supposed to work originally.

This will provide all the variables that would have been passed to the preprocessor or the theme function directly to the template. This is quite evil, dangerous, and powerful.

<?php
function mytheme_theme(&$existing, $type, $theme, $path) {
  foreach (
$existing as $name=>$opts) {
   
$tpl = str_replace('_', '-', $name);
   
$exists = file_scan_directory(path_to_theme(), $tpl .'.tpl.php');
    if (!empty(
$exists)) {
     
$existing[$name]['theme paths'][] = path_to_theme();
     
$existing[$name]['template'] = $tpl;
    }
  }
  return array(
// ...Regular registration code... );
}
?>

Passing in $existing by reference allows us to muck about with the theme registry before it is written to the database. Someone more evil than I might imagine other applications for this trick.

Login to post comments

Oooh yes, evil indeed. Nice

moshe weitzman's picture
moshe weitzman - Sun, 2008-08-31 20:26

Oooh yes, evil indeed. Nice one.