Drupal 7 hook_theme() not loading template file

drupaldrupal-7drupal-themesdrupal-theming

I'm trying to get a very simple module to load a template file using drupal's hook_theme(). It's pretty much as simple as you can possibly imagine.

function sectionheader_theme ( $existing, $type, $theme, $path ) {
  return array(
    'sectionheader' => array(
      'variables' => array( 'foo' => NULL ),
      'template' => 'sectionheader',
    ),
  );
}

The template is named sectionheader.tpl.php. The rest of the module is working as expected. I've cleared the Drupal cache. I've inserted a die("Debug") statement in this function, and it is being executed, but my template is simply not being called, ever. The template merely has some debug text in it so I can see that it's working, but is not visible in any view of the module.

I've done everything in every example I can find, I've even copied and pasted code directly from other modules, and this template will still not load.

Best Answer

Note, if you have put your template file in a /theme subfolder in your module dir ( which is best practice), you'll also need to specify the file path in hook_theme

function example_theme($existing, $type, $theme, $path) {
  return array(
    'example_function' => array(
      'variables' => array('var1' => array(), 'var2' => array(), 'var3' => array()),
      'template' => 'example-template',
      'path' => drupal_get_path('module', 'example').'/theme'
    ), 
  );  
}
Related Topic