如何使我的模块模板文件在 Drupal 7 中呈现?

发布于 2024-11-28 10:51:32 字数 986 浏览 0 评论 0原文

我觉得我一定很接近了,但是距离正确渲染我的第一个 Drupal 模块还差一点(当然下一步是通过 ajax 提交和处理表单,我猜我还没有完全退出)不管怎样

,通过阅读“Pro Drupal Development 7”、阅读堆栈并提出一些问题,我已经完成了我的 Drupal 模块的设置。

 \sites\all\modules\custom\mymodule
  ---- mymodule.info
  ---- mymodule.css
  ---- mymodule.js
  ---- mymodule.tpl
  ---- mymodule.module

所有的要求似乎都可以使模块进入我的块列表、CSS 和 & 中。如果我选择查看源代码,JS 在页面上可见。

出于测试目的,我的 TPL 文件仅包含以下内容。

 <h1> HERE </h1>

模块文件(据我所知,这是我编写主题挂钩的地方)具有以下(根据我收集的)关键功能。

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'file'      => 'mymodule', 
        'path'      => drupal_get_path('module', 'mymodule') 
    )
  );
}

function mymodule_page(){

  $content = "";
  return theme('mymodule', $content); 
}

我很确定我只是遇到了一个相对简单的小问题 - 但我已经独自努力了一个小时。有谁知道答案,我在这里缺少什么?

谢谢!

I'm feeling like I must be really close, but am just a little off from rendering out the my first Drupal module correctly (though of course the next step is submitting and processing the form via ajax,guess I'm not quite out of the woods yet)

Anyways, from following along through "Pro Drupal Development 7", reading and asking some questions here on stack, I've gotten to the point where I've got my Drupal module set up.

 \sites\all\modules\custom\mymodule
  ---- mymodule.info
  ---- mymodule.css
  ---- mymodule.js
  ---- mymodule.tpl
  ---- mymodule.module

All of the requirements seem to be functioning to get the module in my blocks list, the CSS, & JS are visible on page if I choose to view source.

For testing purposes, my TPL file just has the following in it.

 <h1> HERE </h1>

The module file (from what I understand, it's the place where I write my theme hook) has the following (from what I gather) key functions.

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'file'      => 'mymodule', 
        'path'      => drupal_get_path('module', 'mymodule') 
    )
  );
}

function mymodule_page(){

  $content = "";
  return theme('mymodule', $content); 
}

I'm pretty sure I'm just having a small, relatively simple issue - but have been hammering away for an hour on my own. Does anyone know the answer, what am I missing here ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-12-05 10:51:32

嘿,任何将来发现这个的人......事实证明,在 drupal 中渲染页面视图的函数是 _block-view content 就是被渲染的内容。

function mymodule_block_view($delta = '') {
$build['mymodule'] = array(
    '#theme' => 'mymodule',
    '#title' => "FOO"
);

$block['subject'] = t('Title of second block (example_empty)');
// $block['content'] = drupal_render($build); // this was wrong
$block['content'] = $build;

return $block;

}

中的代码找到了示例

我通过下载并逐步执行http://drupal.org/project/examples

特别解压缩并安装此 http://ftp.drupal.org/files/projects/examples-7.x-1.x-dev.zip/sites/all/modules 并且您可以在单步执行代码的同时在您自己的 drupal 站点上查看实时示例。

注意:下面 NMC 的答案是正确的,因为模块文件必须重命名为 mymodule.tpl.php

编辑 根据下面的评论,不需要对注释掉的行进行 drupal_render 调用。它在一种环境中有效,但在另一种环境中无效。为了保持一致性,最好不要这样做。

Hey anyone who finds this in the future... As it turns out, the function that renders a view to the page in drupal is <modulename>_block-view the content of that block is what gets rendered.

function mymodule_block_view($delta = '') {
$build['mymodule'] = array(
    '#theme' => 'mymodule',
    '#title' => "FOO"
);

$block['subject'] = t('Title of second block (example_empty)');
// $block['content'] = drupal_render($build); // this was wrong
$block['content'] = $build;

return $block;

}

I found the example from downloading and stepping through the code in

http://drupal.org/project/examples

specifically unzip and install this http://ftp.drupal.org/files/projects/examples-7.x-1.x-dev.zip to /sites/all/modules and you can see live example on your own drupal site while stepping through the code.

note: the answer from NMC below was correct in that the module file had to be renamed to mymodule.tpl.php

EDIT as per comment below, the drupal_render call on the commented out line was not required. It worked in one environment, but not in another. Best not to do it for consistency.

浅唱々樱花落 2024-12-05 10:51:32

尝试

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'template'  => 'mymodule', 
    )
  );
}

将您的 mymodule.tpl 重命名为 mymodule.tpl.php

Try

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'template'  => 'mymodule', 
    )
  );
}

and rename your mymodule.tpl to mymodule.tpl.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文