2012-02-12 122 views
0

我使用Drupal 7並創建了一個名爲fb的自定義模塊。在fb.module文件,我有以下幾點:不明白爲什麼hook_theme()似乎沒有做任何事情

function fb_theme($existing, $type, $theme, $path) { 
    return array(
    'fb' => array(
     'template' => 'fb' 
    ) 
); 
} 

在同一目錄模塊文件(該模塊的根),我有一個名爲fb.tpl.php一個文件,其中包含:

fb.tpl.php is working! 

出於測試目的我的主題的html.tpl.php文件調用在身體下面:

<?php 
$ouput = theme('fb'); 
print_r($output); 
?> 

然而,print_r($output)線不產生nything。我期望它包含fb.tpl.php文件的內容,或者包含該文件內容的數組作爲其參數之一的值。爲什麼不呢?

回答

1

你並不需要在Drupal 7在所有使用的主題功能相反,創建一個可呈現數組是這樣的:

$output = array(
    '#theme' => 'fb' 
); 

,並輸出這樣的:

drupal_render($output); 

那將是在html.tpl.php文件中輸出它的最簡單的方法。

-1

您正在使用drupal 6語法。在D7語法如下:

function fb_theme($existing, $type, $theme, $path) { 
    return array(
    'fb' => array(
     'file' => 'fb' 
    ) 
); 
} 

看到完整的文檔在這裏:http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7

[編輯] - 也不要忘記您更改主題掛機後刷新主題緩存,否則你不會看到變化。

+0

他的語法是正確的。 '文件'鍵指定了函數實現的文件,而不是主題實現的模板文件。 'template'鍵是在這種情況下用來向Drupal發送它應該使用的tpl.php文件的正確信號。 – KerrM 2012-02-13 14:03:54

相關問題