如何在 drupal 7 中实现 hook_theme?
我创建了一个新的 drupal 7 主题并尝试在 template.php 中实现 hook_theme,如下所示:
I created a new drupal 7 theme and trying to implement hook_theme at template.php like this:
function mytheme_theme($existing, $type, $theme, $path){
return array(
'mytheme_header'=>array(
'template'=>'header',
'path'=>$path.'/templates',
'type'=>'theme',
),
);
}
然后我把header.tpl.php放入templates目录并清除所有缓存,并调用主题函数:
then I placed header.tpl.php into templates directory and cleared all caches, and call theme function:
theme('mytheme_header', $vars);
和 header.tpl.php 喜欢这个:
and header.tpl.php likes this:
<?php
fb('calling header template');//the function of FirePHP to output debug info
print '<div>Header</div>';
//...
我检查了 Firebug,它得到信息调用标题模板",这意味着它调用了 header.tpl.php,但它没有打印 html 代码.我的代码有什么问题?
I check Firebug and it get the info 'calling header template', it mean it had called header.tpl.php, but it didn't print the html code. What's wrong with my code?
推荐答案
尝试在hook_theme
function mytheme_theme($existing, $type, $theme, $path){
return array(
'mytheme_header' => array(
'template' => 'header',
'path' => $path . '/templates',
'type' => 'theme',
'variables' => array(
'title' => NULL,
'some_text' => NULL,
),
),
);
}
在您的 header.tpl.php
文件中:
<h1><?php print $title; ?></h1>
<p><?php print $some_text; ?></p>
然后,像这样打印出来:
Then, print it out like this:
$vars = array();
$vars['title'] = "This is a title";
$vars['some_text'] = "Some text...";
print theme('mytheme_header', $vars);
相关文章