国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

Drupal中hook_theme函數(shù)用法

2024-08-30 19:07:49
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Drupal中hook_theme函數(shù)用法。分享給大家供大家參考。具體如下:

在開發(fā)的時(shí)候不免要使用到drupal theme 定義。
舉個(gè)簡(jiǎn)單的例子:

<?php
function modulename_theme() { //開始定義自己的theme 使用api hook_theme
return array( //返回theme 數(shù)組
'hot_news' => array( // 給定義的theme 定義一個(gè)名稱
'arguments' => array('title' => NULL, 'teaser' => NULL, 'link' => NULL), //這些都是要傳遞的參數(shù),具體是在使用 theme('hot_news',arg1,arg2,arg3),這時(shí)使用到。
'template' => 'hot_news', //模板名稱,它會(huì)自動(dòng)搜索hot_news.tpl.php模板文件
'file' => 'get_page.inc', //這個(gè)是定義相關(guān)函數(shù)的文件,根據(jù)需要自行定義。
'path' =>drupal_get_path('module', 'modulename'), //得到文件路徑,如果theme('hot_news',arg)在template.php里面使用,需要告訴drupal具體位置,不定義,如果在template使用,它只能在template.php同目錄下查找。默認(rèn)和主題同目錄。
),
);
?>

每個(gè)參數(shù)都會(huì)寫入變量里。 variables.,比如:$variables['title'], $variables['teaser'] and $variables['link'].
接下去就可以使用:

<?php
$output = theme('hot_news', '這是標(biāo)題','haha,teaser','yes, 這是link');//使用這個(gè)時(shí)候,他會(huì)輸出定義的hot_news.tlp.php模板內(nèi)容樣式。、。
?>

還有一個(gè)功能就是預(yù)處理機(jī)制。

<?php
function template_preprocess_hot_news(&$variables) {
// $variables['title'] 的值可以使用 $title 在你的hot_news.tpl.php里面輸出
$variables['title'] = '在處理一次,讓它顯示別的title';
$variables['teaser'] = 'strng......';
$variables['link'] = l(eeeee, 'node/'.1);
}
?>

理解hook_theme,就可以自己隨心所欲來定制自己的theme。感覺到drupal的強(qiáng)大和靈活了。

總結(jié):

當(dāng)告知drupal使用theme('hook',arg)時(shí), 它需要找到hook_theme的定義,如果沒有preprocess,那直接把參數(shù)送給你tpl.php文件里。如果有,它就把theme('hook',arg)的來參數(shù),傳遞給preprocess里面,可以直接用$variables['arg']得到值,看看沒有重新賦值,如果有,那就使用新的$variables['arg'],最后輸出到tpl.php里面。

希望本文所述對(duì)大家的drupal建站有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 隆回县| 葵青区| 莱阳市| 云浮市| 宁城县| 垦利县| 伽师县| 呼图壁县| 买车| 龙井市| 太仆寺旗| 松桃| 东城区| 铜山县| 宁波市| 潮安县| 佛山市| 金溪县| 高淳县| 大关县| 皋兰县| 喜德县| 嘉荫县| 吉木乃县| 青浦区| 滦平县| 高碑店市| 广丰县| 从江县| 吴川市| 临西县| 祥云县| 徐州市| 温州市| 深水埗区| 家居| 石楼县| 新干县| 澄江县| 通河县| 德安县|