在Drupal中注册您的主题功能

时间:2010-11-04 19:29:48

标签: drupal

我是Drupal的新手,我正在为自定义模块创建自己的主题。任何人都可以帮助我了解如何注册我们的主题功能或分享从头开始解释过程的任何想法或链接。

2 个答案:

答案 0 :(得分:29)

注册主题功能意味着在您的模块中实施hook_theme

例如,如果您的模块名为“example”,那么您需要在example_theme文件中使用名为example.module的函数。主题函数必须返回一个数组,否则你最终会得到着名的死亡白屏。


example.module

<?php
// $Id$

// Implements hook_theme
function example_theme(){
    return array(
        'mydata' => array(

            // Optionally, you can make the theme use a template file:
            // this line references the file "mydatafile.tpl.php" in the same folder as the module or in the folder of the active theme
            'template' => 'mydatafile',

            // these variables will appear in the template as $var1 and $var2
            'arguments' => array(
                'var1' => null, 
                'var2' => null, 
            ),

        ),
        'myotherdata' => array(

            // these variables will appear in the functions as the first and second arguments
            'arguments' => array(
                'var1' => null, 
                'var2' => null, 
            ),

        )
    );
}


// If you don't want to use a template file, use a function called "theme_THEID" to create the HTML.
function theme_myotherdata($var1, $var2){
    return "<div>var1= $var1 and var2= $var2</div>";
}

mydatafile.tpl.php

<div>mydatafile.tpl.php was called</div>
<ol>
    <li>var1: <?php echo $var1; ?></li>
    <li>var2: <?php echo $var2; ?></li>
</ol>

如果需要,您可以稍后手动调用主题功能:

 $html = theme('mydata', 'hello world', 123);
 $html = theme('myotherdata', 'hello world', 123);

在这种情况下,“mydatafile.tpl.php”和“theme_myotherdata”将在$ var1中收到值“hello world”,在$ var2中收到值123。


many more options,比如更改函数的名称,使用模式而不是固定名称,能够将函数放在另一个php文件中,请查看链接。


以下是关于主题的更多资源:


顺便说一下,如果在安装后的.module文件中添加函数,则需要重建主题注册表缓存,在这种情况下,您可以通过清除缓存来实现此目的(一种方法是使用按钮)在“演出”页面的底部)。

答案 1 :(得分:4)

注意:Drupal 7的语法略有不同(例如&#39;参数&#39;变更&#39;变量&#39;)

http://www.davidcalculli.com/blog/2012/01/drupal-7-custom-template-only-displaying-the-first-character