Drupal 7 hook_theme() 未加载模板文件 [英] Drupal 7 hook_theme() not loading template file

查看:19
本文介绍了Drupal 7 hook_theme() 未加载模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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',
    ),
  );
}

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

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.

推荐答案

注意,如果您将模板文件放在模块目录的/theme 子文件夹中(这是最佳实践),您还需要指定hook_theme 中的文件路径

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'
    ), 
  );  
}

这篇关于Drupal 7 hook_theme() 未加载模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆