Drupal theme() 函数和自定义模板

标签 drupal

我有一个自定义模块,可以从 Web 服务调用返回数据。它来自 XML 响应,我将其转换为数组。

一旦我有了数组,我就会:

$output = theme('search_srs_results', $data);
return $output;

但是我遇到了白屏。没有 apache/php/watchdog 错误。

我之前曾在另一个模块中毫无困难地完成过此操作。我的主题 Hook 已定义,并指向模板文件,传递 $data 参数。如果我在 $output 返回之前将其转储,则其为 NULL。

$data 在主题化之前肯定有一个填充数组。

如果我执行 theme('item_list', $data);,它会呈现,没有白屏。

我尝试再次阅读有关 hook_theme 和 theme() 的文档,但我似乎没有做任何错误。

以下是主题功能:

/**
 * Implementation of hook_theme()
 */

function srs_finder_theme() {
  return array(
    'search_srs_results' => array(
      'template' => 'srs-finder-results',
      'arguments' => array('data' => null),
    ),
  );
}

/**
 * Implementation of hook_preprocess()
 */

function srs_finder_preprocess_search_srs_results(&$vars) {
  $data = $vars['data'];
}

缺少什么?

最佳答案

我根本不明白为什么你需要 hook_preprocess() 函数。 $data 应自动可供 srs-finder-results.tpl.php 使用。那是因为您在调用 theme('src_src_results', $data) 中传递此变量,并且您已声明 hook_theme() 中有 1 个参数。

srs-finder-results.tpl.php 文件应驻留在 src_finder 模块文件夹中。您需要为此实现代码! (或者,正如 nikit 上面评论的那样,提供一个 theme_search_srs_results 函数。在这种情况下,您将需要删除 template 数组条目)

[注意:如果模块的其他用户想要覆盖此主题模板,他们始终可以在主题的主题文件夹中提供自己的 srs-finder-results.tpl.php 实现,处于事件状态。]

关于Drupal theme() 函数和自定义模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4056497/

相关文章:

database - Drupal 7 - db_insert 返回错误

mysql - Drupal 8 没有连接到 Docker 中的 Mysql

php - 为什么 drupal 很慢?

css - 修复有问题的响应式 CSS Zen 子主题

php - 如何在 drupal 7 中定义模块级变量

php - 如何在注册时自动分配自定义角色?

drupal - 隐藏 Drupal 7 中特定推荐人的菜单链接

Drupal 站点中的 jQuery addClass() 函数在 IE7 中不起作用

MySQL 查询 - 帮助选择

javascript - 当按下链接以在不同页面上的表单中的下拉列表中设置值时