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

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

问题描述

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

谢谢,

解决方案

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

function mymodule_theme($existing, $type, $theme, $path) {返回数组('your_theme_key' =>大批('变量' =>大批('nid' =>空值,'标题' =>空值),'模板' =>'your_template_filename',//不包括 .tpl.php'路径' =>'你的模板文件的路径'));}

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

//使用主题提供的变量定义你的 html 代码<div class="node node-type" id="node-<?php print $nid; ?>"><h3><?php print l($title, "node/{$nid}");?></h3>

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

$variables = array('nid' =>$nid,'标题' =>$title);$output = theme('your_theme_key', $variables);打印 $output;

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?

Thanks,

解决方案

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

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天全站免登陆