主题($ hook,$ variables = array())以及它在Drupal 7中的工作原理 [英] theme($hook, $variables = array()) and how it works in Drupal 7

查看:151
本文介绍了主题($ hook,$ variables = array())以及它在Drupal 7中的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将主题内容输出到一个页面,我一直在尝试阅读theme()函数及其工作原理。据了解,它提供了一种生成主题HTML的方法。这正是我想要的。现在,我不明白的是我如何传递HTML或我想要的变量,以便生成HTML。 $ hook参数是什么?它是一个.tpl.php文件吗?如何构建此文件,以便HTML显示在页面的内容部分?有人可以用简单的方式解释theme()函数吗?

I'm trying to output themed content to a page and I've been trying to read up on the theme() function and how it works. As I understand, it provides a method to generate themed HTML. That's exactly what I want. Now, what I don't understand is how I pass it the HTML or the variables I want so that the HTML is generated. What's the $hook parameter? Is it a .tpl.php file? How do I structure this file so that the HTML is displayed in the content section of the page? Can someone explain the theme() function in a very simple way?

谢谢,

推荐答案

你必须编写自己的模块。在您的模块中,您必须使用 hook_theme 函数定义您的主题。

You have to write your own module. In your module you have to define your theme using hook_theme function.

function mymodule_theme($existing, $type, $theme, $path) {
    return array(
        'your_theme_key' => array(
            'variables' => array(
                'nid' => NULL,
                'title' => NULL
            ),
            'template' => 'your_template_filename', // do not include .tpl.php
            'path' => 'path-to-your-template-file'
        )
    );
}

之后,您应该创建文件 your_template_filename.tpl。您的模块文件夹中的php ,您将有变量 $ nid $ title (在这个例子中)。
您的模板文件将如下所示:

After that you should create file your_template_filename.tpl.php in your module's folder and in that file you would have variables $nid and $title (in this example). Your template file would be look like:

// define your html code using variables provided by theme
<div class="node node-type" id="node-<?php print $nid; ?>">
    <h3><?php print l($title, "node/{$nid}"); ?></h3>
</div>

之后,您可以在您网站的任何模块中使用您的主题。应该这样调用:

After that you can use your theme in any modules in your site. Should be called like that:

$variables = array(
    'nid' => $nid,
    'title' => $title
);
$output = theme('your_theme_key', $variables);
print $output;

这篇关于主题($ hook,$ variables = array())以及它在Drupal 7中的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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