您需要使用「#主題」屬性,例如在下面的代碼中。
$build['dblog_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'admin-dblog'),
'#empty' => t('No log messages available.'),
);
#theme
告訴Drupal需要調用哪個主題函數;其他屬性(如果它們已用於不同的目的)用作變量名稱以傳遞給主題函數或模板文件(如果使用的話)。
在此示例中,theme_table()將收到以下數組。
array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'admin-dblog'),
'empty' => t('No log messages available.'),
);
這甚至可以與真正使用的模板文件的主題函數來完成。
您在hook_theme()中定義了一個主題函數,使用必要的鍵用於使用模板文件的主題函數,如下面的代碼所示。
function mymodule_theme($existing, $type, $theme, $path) {
return array(
'mymodule_sidebar' => array(
'variables' => array('topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL),
'template' => 'mymodule-display',
),
);
}
主題功能將類似於下面的代碼的東西可以再使用。
$page['sidebar_first'] = array(
'#theme' => 'mymodule_sidebar',
'#topics' => array('first topic', 'second topic', 'third topic'),
'#parents' => array('my topics', 'my friend's topics'),
);
沒有得到一個明確的價值將得到默認的hook_theme()
一個報告的變量;在這種情況下,所使用變量的默認值是NULL
。