templates - 德鲁帕尔 : How do I invoke a template in a module?

标签 templates drupal-6

我的自定义模块中有一些非节点数据,我想调用模板来显示数据。该模板可能会重复用于电子邮件输出,我基本上只想引导 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/

相关文章:

c++ - 从 gcc 3.3.3 移植到 4.1.0,C++ 模板,对 undefined reference

c++ - 模板类如何在 C++ 中使用另一个模板类中的类型?

C++类成员变量知道自己的偏移量

c++ - 推导模板函数中的数组大小,传递值与传递引用的差值

php - 是否可以通过javascript调用自定义模块中的drupal函数?

php - 使用不同模板在 Drupal 中拥有 2 个单独的用户注册表单的诀窍是什么?

c++ - boost::lexical_cast 如何只采用一种模板类型?

drupal - 1 个固定产品的 CreditCart 付款(无 PayPal、无 Google Checkout、无 Ubercart)

drupal - 如何在 Drupal 中的 "access denied"页面上持久保存表单数据?

drupal - 安装主题/模块时自动启用 block