我正在尝试将主题内容输出到页面,并且一直在尝试阅读 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/