drupal - theme($hook, $variables = array()) 以及它在 Drupal 7 中的工作原理

标签 drupal

我正在尝试将主题内容输出到页面,并且一直在尝试阅读 theme() 函数及其工作原理。据我了解,它提供了一种生成主题 HTML 的方法。这正是我想要的。现在,我不明白我如何将 HTML 或我想要的变量传递给它,以便生成 HTML。 $hook 参数是什么?它是一个 .tpl.php 文件吗?如何构建此文件以使 HTML 显示在页面的内容部分?有人可以用非常简单的方式解释 theme() 函数吗?

谢谢,

最佳答案

您必须编写自己的模块。在您的模块中,您必须使用 hook_theme 定义您的主题。功能。

function mymodule_theme($existing, $type, $theme, $path) {
    return array(
        'your_theme_key' => array(
            'variables' => array(
                'nid' => NULL,
                'title' => NULL
            ),
            'template' => 'your_template_filename', // do not include .tpl.php
            'path' => 'path-to-your-template-file'
        )
    );
}

之后你应该创建文件 your_template_filename.tpl.php在你的模块文件夹和那个文件中你会有变量$nid$title (在本例中)。
您的模板文件将如下所示:
// define your html code using variables provided by theme
<div class="node node-type" id="node-<?php print $nid; ?>">
    <h3><?php print l($title, "node/{$nid}"); ?></h3>
</div>

之后,您可以在站点的任何模块中使用您的主题。应该这样称呼:
$variables = array(
    'nid' => $nid,
    'title' => $title
);
$output = theme('your_theme_key', $variables);
print $output;

关于drupal - theme($hook, $variables = array()) 以及它在 Drupal 7 中的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8258494/

相关文章:

Drupal:将分类术语名称转换为术语 ID

drupal - 如何调用钩子(Hook)菜单中的节点?

mysql - 手动修改 CCK 表安全吗?

drupal - 如何修复由于 Drupal 中的模块造成的不可逆损坏

Drupal 7 : How do I import a RSS feed?

Drupal:从 db_query 返回 SQL 字符串

php - 使用 Drupal 恢复备份站点

session - 在 session 中存储数据有哪些风险?

javascript - Drupal 分别打印 .info 脚本组