它为模块提供了一个定义其主题的地方,然后可以被任何其他模块/主题覆盖。它还将为任何模块提供使用钩子的机会,例如mymodule_preprocess_theme_name
更改传递给最终主题函数或模板文件的变量。
初始化主题函数基本上有两种方法:
theme('poll_results', array('raw_title' => 'title', 'results' => $results, etc...));
和
$build = array(
'#theme' => 'poll_results',
'#raw_title' => 'title',
'#results' => $results,
etc...
); // Note the '#' at the beginning of the argument name, this tells Drupal's `render` function that this is an argument, not a child element that needs to be rendered.
$content = render($build); // Exact equivalent of calling the previous example now that you have a render array.
请记住,您应该避免直接调用 theme()(根据 theme.inc 中的文档),因为它:
- 规避缓存。
- 规避 hook_element_info() 中定义的类型的默认值,包括附加资产
- 绕过 pre_render 和 post_render 阶段。
- 绕过 JavaScript 状态信息。
在 Drupal 8 中,theme() 是一个私有函数,_theme()。有关更多详细信息,请参阅www.drupal.org/node/2173655。
当您将这两个与poll_results
上面给出的示例中的元素进行比较时,您可能会弄清楚发生了什么......因为 PHP 不是一种强类型语言 Drupal 通过传递给theme
函数的键控数组提供“命名参数” ,或作为渲染数组中的散列键。
就“渲染元素”而言,这基本上告诉主题系统将使用渲染数组调用此主题函数,并带有一个命名参数(在本例中form
)。代码看起来像这样:
$build = array(
'#theme' => 'poll_choices',
'#form' => $form
);
这会将$form
变量中的任何内容作为唯一参数传递给主题函数。
关于template
关键:
'poll_vote' => array(
'template' => 'poll-vote',
'render element' => 'form',
)
定义了一个名为的主题poll_vote
,它使用一个名为“poll-vote.tpl.php”的模板文件(因此是template
密钥)(这是按照惯例)。该模板文件的路径将通过使用实现它的模块的路径来找到(例如 modules/poll/poll-vote.tpl.php),因此可以将模板文件放在主模块文件夹的子文件夹中.
有两种方法可以实际返回主题函数的输出,通过实现物理函数名称(在本例中为theme_poll_vote
)或使用模板文件。如果template
键为空 Drupal 将假定您已经实现了一个物理函数并尝试调用它。
如果您有相当多的 HTML 来为主题输出,或者您只是不喜欢在 PHP 中以字符串形式编写 HTML(我个人不喜欢),则模板文件更可取。theme()
但是,在任何一种情况下,调用主题时传递的变量(如上所述使用或渲染数组)本身都会传递给模板文件或主题函数。所以:
function theme_poll_results(&$vars) {
$raw_title = $vars['raw_title'];
$results = $vars['results'];
// etc...
}
如果您使用模板文件而不是相同的方法,则变量将作为 , 等可用$raw_title
,$results
因为 Drupalextract
在$vars
解析模板文件之前运行。
我敢肯定我在这里错过了很多东西,但是如果您有任何更具体的问题,请尽管提出,我会尽力提供帮助。