在Drupal中注册您的主题功能

Pat*_*tel 4 drupal

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

wil*_*aks 29

注册主题功能意味着在模块中实现hook_theme.

例如,如果您的模块被称为" example",那么您需要example_themeexample.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>";
}
Run Code Online (Sandbox Code Playgroud)

mydatafile.tpl.php中:

<div>mydatafile.tpl.php was called</div>
<ol>
    <li>var1: <?php echo $var1; ?></li>
    <li>var2: <?php echo $var2; ?></li>
</ol>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在需要时手动调用主题功能:

 $html = theme('mydata', 'hello world', 123);
 $html = theme('myotherdata', 'hello world', 123);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,"mydatafile.tpl.php"和"theme_myotherdata"将在$ var1中接收值"hello world",在$ var2中接收值123.


还有更多选项,比如更改函数名称,使用模式而不是固定名称,能够将函数放在另一个php文件中,请查看链接.


这里有几个关于主题的资源:


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