drupal theme() api

本文将介绍如何在Drupal中使用themeapi进行自定义主题注册、输出样式定义及预处理过程,通过实例深入理解主题开发流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常会使用 theme api,比如 theme('image',xxxx),theme('item_list', $items);等等。有事需要自己定义这样的theme. 简单步骤:
1 在使用hook_theme 注册登记一下。叫什么名字。

[php]  view plain copy
  1. <?php  
  2. /** 
  3. *注册theme 
  4. */  
  5. function  modulename_theme() {  
  6.   return array(  
  7.     'hot_news' => array(  
  8.       'arguments' => array('title' => NULL),//传递给模板里的参数,数字型,对象,数组等等     
  9.       'template' => 'hot-news',//模板名称,不需要加.tpl.php,程序会自动检索。如果不指定模板路径,默认搜索模块同位置    
  10.     ),  
  11.   
  12.     'good_news' => array(  
  13.       'arguments' => array('cotent' => NULL),  
  14.     ),  
  15.   
  16.   );  
  17. }  
  18. ?>  

2、定义theme的输出格式

[php]  view plain copy
  1. <?php  
  2.   
  3. function theme_good_news($content){  
  4. return '<div>'.$content .'</div>';  
  5. }  
  6. ?>  


3 也可以使用preprocess来先定义好变量。
[php]  view plain copy
  1. <?php  
  2.   
  3. function template_preprocess_hot_news($variables){  
  4. $variables['output'] = $content;  
  5. }  
  6. ?>  


4.这样你就可以使用theme('hot_news',$var);来处理你想要的数据了。

[php]  view plain copy
  1. <?php  
  2.   
  3. $output = "xxxxx";  
  4. print theme('hot_news',$output);  
  5. ?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值