【问题标题】:How to make a Drupal 7 module use up a .tpl.php template in the theme folder如何使 Drupal 7 模块用完主题文件夹中的 .tpl.php 模板
【发布时间】:2011-06-05 16:25:06
【问题描述】:

我为 Drupal 7 创建了一个模块,它有一个 hook_theme 函数,告诉它使用 usertemp.tpl.php 模板。我将模板放在模块文件夹和主题文件夹中。问题是该功能仅从模块文件夹中获取模板,而不是从主题文件夹中获取模板。我已经反复清除缓存并寻找以前的答案,但没有任何帮助。可能是什么问题?

我的 hook_theme 代码如下所示:

function usuar_theme() {
  return array(
    'usuarbuild' => array(
        'variables' => array('profilesloaded' => array()),
        'template' => 'usertemp',
     ),
  );
}

剩下的模块代码是这样的:

function usuar_menu() {
    $items['userx'] = array(
       'title' => 'User page',
       'description' => 'User page',
       'page callback' => 'usuar_exe',
       'access callback' => TRUE,
       'type' => MENU_CALLBACK,
     );
     return $items;
}


function usuar_exe($id) {
    $ar = array('uid' => $id, 'profilesloaded' => profile2_load_by_user($id));
    return theme('usuarbuild', array('collected' => $ar));
}

function theme_usuarbuild($variables) {
  return $variables['collected'];
}

【问题讨论】:

  • 对我来说看起来不错,你能在模块中包含你用来调用模板的代码吗?
  • 我以为这是用来调用模板的代码,不是吗?你的意思是自定义主题函数和页面回调?
  • 我已经用其余的模块代码编辑了我的主要帖子。感谢您的帮助!

标签: drupal drupal-modules drupal-7 drupal-themes


【解决方案1】:

这是一个棘手的问题,但是.. 您的主题挂钩必须与您的模板名称匹配。很奇怪,但是我在本地进行了测试,并且一旦我以这种方式设置它就可以工作。所以.. 将您的 hook_theme() 更改为:

function usuar_theme() {
  return array(
    'usuarbuild' => array(
        'variables' => array('profilesloaded' => array()),
        'template' => 'usuarbuild',
     ),
  );
}

并将您的 tpl.php 文件更改为 usuarbuild.tpl.php(或将所有内容更改为 usertemp)。之后应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多