WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計算
- 微博營銷
- 虛擬主機管理系統(tǒng)
- 開放平臺
- WIKI程序與應(yīng)用
- 美國十大主機
Drupal/Drupal hook theme使用
來自站長百科
模板:Drupal top 在開發(fā)的時候不免要使用到drupal theme定義。
Drupal hook theme使用[ ]
簡單的例子:
<?php function modulename_theme() { //開始定義自己的theme 使用api hook_theme return array( //返回theme 數(shù)組 'hot_news' => array( // 給定義的theme 定義一個名稱 'arguments' => array('title' => NULL, 'teaser' => NULL, 'link' => NULL), //這些都是要傳遞的參數(shù), 具體是在使用theme('hot_news',arg1,arg2,arg3),這時使用到。 'template' => 'hot_news', //模板名稱,它會自動搜索hot_news.tpl.php模板文件 'file' => 'get_page.inc', //這個是定義相關(guān)函數(shù)的文件,根據(jù)需要自行定義。 'path' =>drupal_get_path('module', 'modulename'), //得到文件路徑,如果theme('hot_news',arg)在template.php里面使用, 需要告訴drupal具體位置,不定義,如果在template使用,它只能在template.php同目錄下查找。默認和主題同目錄。 ), ); ?>
每個參數(shù)都會寫入變量里。 variables.,比如:$variables['title'], $variables['teaser'] and $variables['link'].
接下去就可以使用:
<?php $output = theme('hot_news', '這是標(biāo)題','haha,teaser','yes, 這是link');//使用這個時候,他會輸出定義的hot_news.tlp.php模板內(nèi)容樣式。、。 ?>
還有一個功能就是預(yù)處理機制。
<?php /** * template_preprocess_hook($variables) //也就是,自己可以控制模板內(nèi)容輸出的,比如title,先根據(jù)傳遞過來的值,做一下處理,然后再 用$variables['title'] 輸出。 模板里面只可以可以使用print $title */ 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的強大和靈活了。
總結(jié)[ ]
當(dāng)告知drupal使用theme('hook',arg)時, 它需要找到hook_theme的定義,如果沒有preprocess,那直接把參數(shù)送給你tpl.php文件里。如果有,它就把theme('hook',arg)的來參數(shù),傳遞給preprocess里面,可以直接用$variables['arg']得到值,看看沒有重新賦值,如果有,那就使用新的$variables['arg'],最后輸出到tpl.php里面。