加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在drupal 7中实现hook_theme?

发布时间:2020-12-13 17:29:27 所属栏目:PHP教程 来源:网络整理
导读:我创建了一个新的drupal 7主题,并尝试在template.php上实现hook_theme,如下所示: function mytheme_theme($existing,$type,$theme,$path){ return array( 'mytheme_header'=array( 'template'='header','path'=$path.'/templates','type'='theme',),);} 然
我创建了一个新的drupal 7主题,并尝试在template.php上实现hook_theme,如下所示:
function mytheme_theme($existing,$type,$theme,$path){
    return array(
        'mytheme_header'=>array(
            'template'=>'header','path'=>$path.'/templates','type'=>'theme',),);
}

然后我将header.tpl.php放入templates目录并清除所有缓存,并调用主题函数:

theme('mytheme_header',$vars);

和header.tpl.php喜欢这样:

<?php
fb('calling header template');//the function of FirePHP to output debug info
print '<div>Header</div>';
//...

我检查Firebug,它得到的信息’调用头模板’,这意味着它已经调用了header.tpl.php,但它没有打印html代码.我的代码有什么问题?

尝试在hook_theme中添加变量数组
function mytheme_theme($existing,$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>

然后,打印出来像这样:

$vars = array();
$vars['title'] = "This is a title";
$vars['some_text'] = "Some text...";
print theme('mytheme_header',$vars);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读