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

查看:25
本文介绍了如何在 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);

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

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