我正在尝试将主题内容输出到页面,我一直在尝试阅读theme()函数及其工作原理.据我了解,它提供了一种生成主题HTML的方法.这正是我想要的.现在,我不明白的是我如何传递HTML或我想要的变量,以便生成HTML.什么是$ hook参数?它是.tpl.php文件?如何构建此文件以便HTML显示在页面的内容部分?有人能用一种非常简单的方式解释theme()函数吗?
谢谢,
Vla*_*lat 11
你必须编写自己的模块.在您的模块中,您必须使用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'
)
);
}
Run Code Online (Sandbox Code Playgroud)
之后,你应该创建文件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>
Run Code Online (Sandbox Code Playgroud)
之后,您可以在您网站的任何模块中使用您的主题.应该这样称呼:
$variables = array(
'nid' => $nid,
'title' => $title
);
$output = theme('your_theme_key', $variables);
print $output;
Run Code Online (Sandbox Code Playgroud)