我的自定义模块中有一些非节点数据,我想调用模板来显示数据。该模板可能会重复用于电子邮件输出,我基本上只想引导 Drupal 并根据需要使用该模板。
我想我可以做这样的事情:
$html = theme('verysmallsnippet', $my_fancy_data_structure);
...然后在事件主题文件夹中创建一个 verysmallsnippet.tpl.php
文件,然后期望 Drupal 找到模板文件并使用传递给主题函数的参数评估模板。这是对模板引擎如何工作的过于简单化的解释——还是我需要先设置主题注册表?或者是什么?
我查看了this question ,以及theme文档,我有点迷失了。
最佳答案
是的,有点过于简单化了;)
为了比您链接的 API 文档更好的介绍,请从 Theming Guide 开始,并根据您的具体需求,检查 Using the theme layer (Drupal 6.x) .
为了使您的示例正常工作,您需要通过实现 hook_theme() 告诉主题注册表有关您的模板的信息。 :
function yourModule_theme($existing, $type, $theme, $path) {
$items = array();
$items['verysmallsnippet'] = array(
'arguments' => array('my_fancy_data_structure' => array()), // Change array() to a fitting default
'template' => 'verysmallsnippet',
);
return $items;
}
您可以在该 Hook 中定义更多详细信息,但这是获取使用的模板文件所需的最低限度(引擎会在查找文件时为您添加“tpl.php”部分)。
注意:每次添加或更改 hook_theme() 实现时都需要触发主题注册表的重建,否则您的更改将不会生效。
关于templates - 德鲁帕尔 : How do I invoke a template in a module?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4148462/