Drupal 7 hook_theme()没有加载模板文件

Jap*_*ape 3 drupal drupal-7 drupal-themes drupal-theming

我正在尝试使用drupal的hook_theme()来获取一个非常简单的模块来加载模板文件.它几乎和你想象的一样简单.

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

该模板名为sectionheader.tpl.php.该模块的其余部分正在按预期工作.我已经清除了Drupal缓存.我在这个函数中插入了一个die("Debug")语句,它正在被执行,但我的模板根本就没有被调用过.模板中只有一些调试文本,所以我可以看到它正在工作,但在模块的任何视图中都不可见.

我已经完成了我能找到的每个例子中的所有内容,我甚至直接从其他模块复制并粘贴了代码,这个模板仍然无法加载.

Dav*_*mas 5

注意,如果您将模板文件放在模块目录中的/ theme子文件夹中(这是最佳实践),您还需要在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'
    ), 
  );  
}
Run Code Online (Sandbox Code Playgroud)