如何在drupal 7中实现hook_theme? [英] How to implement hook_theme in drupal 7?

查看:75
本文介绍了如何在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 file:

<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);

这篇关于如何在drupal 7中实现hook_theme?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆